From 7ca9f3d5bb32b45733ed16c54264823adf03aebf Mon Sep 17 00:00:00 2001 From: Daiderd Jordan Date: Mon, 15 May 2017 08:34:53 +0200 Subject: [PATCH] launchd: add script option for services --- modules/launchd/default.nix | 16 ++++++++-- modules/services/activate-system.nix | 44 ++++++++++++---------------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/modules/launchd/default.nix b/modules/launchd/default.nix index 3a7bfea..7505937 100644 --- a/modules/launchd/default.nix +++ b/modules/launchd/default.nix @@ -5,6 +5,8 @@ with lib; let + inherit (pkgs) stdenv; + cfg = config.launchd; toEnvironmentText = name: value: { @@ -51,6 +53,12 @@ let description = "Command executed as the service's main process."; }; + script = mkOption { + type = types.lines; + default = ""; + description = "Shell commands executed as the service's main process."; + }; + # preStart = mkOption { # type = types.lines; # default = ""; @@ -75,6 +83,12 @@ let }; config = { + command = mkIf (config.script != "") (pkgs.writeScript "${name}-start" '' + #! ${stdenv.shell} + + ${config.script} + ''); + serviceConfig.Label = mkDefault "org.nixos.${name}"; serviceConfig.ProgramArguments = mkIf (cmd != "") [ "/bin/sh" "-c" "exec ${cmd}" ]; serviceConfig.EnvironmentVariables = mkIf (env != {}) env; @@ -85,7 +99,6 @@ in { options = { - launchd.envVariables = mkOption { type = types.attrsOf (types.either types.str (types.listOf types.str)); default = {}; @@ -161,7 +174,6 @@ in 5. When the user logs out, it sends a SIGTERM signal to all of the user agents that it started. ''; }; - }; config = { diff --git a/modules/services/activate-system.nix b/modules/services/activate-system.nix index 5ee7cfb..445c78b 100644 --- a/modules/services/activate-system.nix +++ b/modules/services/activate-system.nix @@ -8,42 +8,34 @@ let cfg = config.services.activate-system; - activateScript = pkgs.writeScript "activate-system" '' - #! ${stdenv.shell} - - # Make this configuration the current configuration. - # The readlink is there to ensure that when $systemConfig = /system - # (which is a symlink to the store), /run/current-system is still - # used as a garbage collection root. - ln -sfn $(cat ${config.system.profile}/systemConfig) /run/current-system - - # Prevent the current configuration from being garbage-collected. - ln -sfn /run/current-system /nix/var/nix/gcroots/current-system - - ${config.system.activationScripts.nix.text} - ''; - in { options = { - services.activate-system = { - - enable = mkOption { - type = types.bool; - default = false; - description = '' - Whether to activate system at boot time. - ''; - }; - + services.activate-system.enable = mkOption { + type = types.bool; + default = false; + description = '' + Whether to activate system at boot time. + ''; }; }; config = mkIf cfg.enable { launchd.daemons.activate-system = { - command = activateScript; + script = '' + # Make this configuration the current configuration. + # The readlink is there to ensure that when $systemConfig = /system + # (which is a symlink to the store), /run/current-system is still + # used as a garbage collection root. + ln -sfn $(cat ${config.system.profile}/systemConfig) /run/current-system + + # Prevent the current configuration from being garbage-collected. + ln -sfn /run/current-system /nix/var/nix/gcroots/current-system + + ${config.system.activationScripts.nix.text} + ''; serviceConfig.RunAtLoad = true; serviceConfig.KeepAlive.SuccessfulExit = false; };