From 2b0aea17934fda4aca24d4b6b99f0b9c24486ff7 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 18 Nov 2013 16:51:39 +0100 Subject: Allow running NixOS services outside of systemd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The attribute ‘config.systemd.services..runner’ generates a script that runs the service outside of systemd. This is useful for testing, and also allows NixOS services to be used outside of NixOS. For instance, given a configuration file foo.nix: { config, pkgs, ... }: { services.postgresql.enable = true; services.postgresql.package = pkgs.postgresql92; services.postgresql.dataDir = "/tmp/postgres"; } you can build and run PostgreSQL as follows: $ nix-build -A config.systemd.services.postgresql.runner -I nixos-config=./foo.nix $ ./result This will run the service's ExecStartPre, ExecStart, ExecStartPost and ExecStopPost commands in an appropriate environment. It doesn't work well yet for "forking" services, since it can't track the main process. It also doesn't work for services that assume they're always executed by root. --- nixos/modules/services/databases/postgresql.nix | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'nixos/modules/services/databases') diff --git a/nixos/modules/services/databases/postgresql.nix b/nixos/modules/services/databases/postgresql.nix index 73447e3cf0d8..a1ab1c92b8f9 100644 --- a/nixos/modules/services/databases/postgresql.nix +++ b/nixos/modules/services/databases/postgresql.nix @@ -181,8 +181,13 @@ in # Initialise the database. if ! test -e ${cfg.dataDir}; then mkdir -m 0700 -p ${cfg.dataDir} - chown -R postgres ${cfg.dataDir} - su -s ${pkgs.stdenv.shell} postgres -c 'initdb -U root' + if [ "$(id -u)" = 0 ]; then + chown -R postgres ${cfg.dataDir} + su -s ${pkgs.stdenv.shell} postgres -c 'initdb -U root' + else + # For non-root operation. + initdb + fi rm -f ${cfg.dataDir}/*.conf touch "${cfg.dataDir}/.first_startup" fi -- cgit 1.4.1