summary refs log tree commit diff
path: root/nixos/modules/services/databases
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2013-11-18 16:51:39 +0100
committerEelco Dolstra <eelco.dolstra@logicblox.com>2013-11-18 18:04:17 +0100
commit2b0aea17934fda4aca24d4b6b99f0b9c24486ff7 (patch)
treeec136362f04cc427ece1d2c1c24a1fc2d38fde5d /nixos/modules/services/databases
parentdc87f8e0809d2c4453b8f6460e33a879af098574 (diff)
downloadnixlib-2b0aea17934fda4aca24d4b6b99f0b9c24486ff7.tar
nixlib-2b0aea17934fda4aca24d4b6b99f0b9c24486ff7.tar.gz
nixlib-2b0aea17934fda4aca24d4b6b99f0b9c24486ff7.tar.bz2
nixlib-2b0aea17934fda4aca24d4b6b99f0b9c24486ff7.tar.lz
nixlib-2b0aea17934fda4aca24d4b6b99f0b9c24486ff7.tar.xz
nixlib-2b0aea17934fda4aca24d4b6b99f0b9c24486ff7.tar.zst
nixlib-2b0aea17934fda4aca24d4b6b99f0b9c24486ff7.zip
Allow running NixOS services outside of systemd
The attribute ‘config.systemd.services.<service-name>.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.
Diffstat (limited to 'nixos/modules/services/databases')
-rw-r--r--nixos/modules/services/databases/postgresql.nix9
1 files changed, 7 insertions, 2 deletions
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