From 264a9237ddaa451efcd77e5fc2d9f78598447caf Mon Sep 17 00:00:00 2001 From: Daiderd Jordan Date: Sun, 14 May 2017 00:26:22 +0200 Subject: [PATCH] launchd: add options for setting launchd environment variables Fixes #12 --- modules/launchd/default.nix | 28 ++++++++++++++++++++++++++++ modules/system/launchd.nix | 10 +++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/modules/launchd/default.nix b/modules/launchd/default.nix index 078bfff1..3a7bfea3 100644 --- a/modules/launchd/default.nix +++ b/modules/launchd/default.nix @@ -86,6 +86,34 @@ in { options = { + launchd.envVariables = mkOption { + type = types.attrsOf (types.either types.str (types.listOf types.str)); + default = {}; + example = { LANG = "nl_NL.UTF-8"; }; + description = '' + A set of environment variables to be set on all future + processes launched by launchd in the caller's context. + The value of each variable can be either a string or a list of + strings. The latter is concatenated, interspersed with colon + characters. + ''; + apply = mapAttrs (n: v: if isList v then concatStringsSep ":" v else v); + }; + + launchd.user.envVariables = mkOption { + type = types.attrsOf (types.either types.str (types.listOf types.str)); + default = {}; + example = { LANG = "nl_NL.UTF-8"; }; + description = '' + A set of environment variables to be set on all future + processes launched by launchd in the caller's context. + The value of each variable can be either a string or a list of + strings. The latter is concatenated, interspersed with colon + characters. + ''; + apply = mapAttrs (n: v: if isList v then concatStringsSep ":" v else v); + }; + launchd.agents = mkOption { default = {}; type = types.attrsOf (types.submodule serviceOptions); diff --git a/modules/system/launchd.nix b/modules/system/launchd.nix index b667efaf..2960df80 100644 --- a/modules/system/launchd.nix +++ b/modules/system/launchd.nix @@ -11,6 +11,10 @@ let mkTextDerivation = pkgs.writeText; }; + launchdVariables = mapAttrsToList (name: value: '' + launchctl setenv ${name} '${value}' + ''); + launchdActivation = basedir: target: '' if ! diff '${cfg.build.launchd}/Library/${basedir}/${target}' '/Library/${basedir}/${target}' &> /dev/null; then if test -f '/Library/${basedir}/${target}'; then @@ -86,7 +90,9 @@ in system.activationScripts.launchd.text = '' # Set up launchd services in /Library/LaunchAgents and /Library/LaunchDaemons - echo "setting up launchd services..." + echo "setting up launchd services..." >&2 + + ${concatStringsSep "\n" (launchdVariables config.launchd.envVariables)} ${concatMapStringsSep "\n" (attr: launchdActivation "LaunchAgents" attr.target) launchAgents} ${concatMapStringsSep "\n" (attr: launchdActivation "LaunchDaemons" attr.target) launchDaemons} @@ -112,6 +118,8 @@ in # Set up user launchd services in ~/Library/LaunchAgents echo "setting up user launchd services..." + ${concatStringsSep "\n" (launchdVariables config.launchd.user.envVariables)} + ${concatMapStringsSep "\n" (attr: userLaunchdActivation attr.target) userLaunchAgents} for f in $(ls /run/current-system/user/Library/LaunchAgents); do