Compare commits

...

2 commits

Author SHA1 Message Date
Robert Helgesson 61475c4779
experiment 2021-02-04 23:53:19 +01:00
Robert Helgesson fa8692f84c
home-environment: make sessionVariables a freeform module
Specifically, introduce explicit options for `PATH` and `NIX_PATH`.
2021-02-04 23:53:18 +01:00
5 changed files with 105 additions and 3 deletions

View file

@ -229,7 +229,30 @@ in
home.sessionVariables = mkOption { home.sessionVariables = mkOption {
default = {}; default = {};
type = types.attrs; type = types.submodule {
freeformType = with types;
lazyAttrsOf (oneOf [ package str int float ]);
options = {
PATH = mkOption {
type = types.envVar;
default = "$PATH";
defaultText = literalExample ''"$PATH"'';
description = ''
Content of the <envar>PATH</envar> variable.
'';
};
NIX_PATH = mkOption {
type = types.envVar;
default = "$NIX_PATH";
defaultText = literalExample ''"$NIX_PATH"'';
description = ''
Content of the <envar>NIX_PATH</envar> variable.
'';
};
};
};
example = { EDITOR = "emacs"; GS_OPTIONS = "-sPAPERSIZE=a4"; }; example = { EDITOR = "emacs"; GS_OPTIONS = "-sPAPERSIZE=a4"; };
description = '' description = ''
Environment variables to always set at login. Environment variables to always set at login.
@ -456,7 +479,7 @@ in
if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi
export __HM_SESS_VARS_SOURCED=1 export __HM_SESS_VARS_SOURCED=1
${config.lib.shell.exportAll cfg.sessionVariables} ${config.lib.shell.exportAll' { colonVars = ["NIX_PATH" "PATH"]; } cfg.sessionVariables}
'' + lib.optionalString (cfg.sessionPath != [ ]) '' '' + lib.optionalString (cfg.sessionPath != [ ]) ''
export PATH="$PATH''${PATH:+:}${concatStringsSep ":" cfg.sessionPath}" export PATH="$PATH''${PATH:+:}${concatStringsSep ":" cfg.sessionPath}"
'' + cfg.sessionVariablesExtra; '' + cfg.sessionVariablesExtra;

View file

@ -4,8 +4,25 @@ rec {
# Produces a Bourne shell like variable export statement. # Produces a Bourne shell like variable export statement.
export = n: v: ''export ${n}="${toString v}"''; export = n: v: ''export ${n}="${toString v}"'';
export' = { colonVars ? [ ] }:
n: v:
let
replaceMatch = match:
lib.replaceStrings [ ":\$${match}:" ":\$${match}" "\$${match}:" ] [
"\${${match}:+:\$${match}:}"
"\${${match}:+:\$${match}}"
"\${${match}:+\$${match}:}"
];
mkValue = n: v:
if builtins.elem n colonVars then replaceMatch n v else toString v;
in ''export ${n}="${mkValue n v}"'';
# Given an attribute set containing shell variable names and their # Given an attribute set containing shell variable names and their
# assignment, this function produces a string containing an export # assignment, this function produces a string containing an export
# statement for each set entry. # statement for each set entry.
exportAll = vars: lib.concatStringsSep "\n" (lib.mapAttrsToList export vars); exportAll = vars: lib.concatStringsSep "\n" (lib.mapAttrsToList export vars);
exportAll' = opts: vars:
lib.concatStringsSep "\n" (lib.mapAttrsToList (export' opts) vars);
} }

View file

@ -1,4 +1,5 @@
{ {
home-session-variables = ./session-variables.nix;
home-session-path = ./session-path.nix; home-session-path = ./session-path.nix;
home-session-variables = ./session-variables.nix;
home-session-variables-explicit = ./session-variables-explicit.nix;
} }

View file

@ -0,0 +1,57 @@
# Test of explicitly defined options inside the `home.sessionVariables` freeform
# module.
{ config, lib, pkgs, ... }:
let
inherit (pkgs.stdenv.hostPlatform) isDarwin;
linuxExpected = ''
# Only source this once.
if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi
export __HM_SESS_VARS_SOURCED=1
export LOCALE_ARCHIVE_2_27="${pkgs.glibcLocales}/lib/locale/locale-archive"
export NIX_PATH="testpath=$HOME/testpath''${NIX_PATH:+:$NIX_PATH}"
export PATH="''${PATH:+$PATH:}$HOME/bin"
export XDG_CACHE_HOME="/home/hm-user/.cache"
export XDG_CONFIG_HOME="/home/hm-user/.config"
export XDG_DATA_HOME="/home/hm-user/.local/share"
'';
darwinExpected = ''
# Only source this once.
if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi
export __HM_SESS_VARS_SOURCED=1
export NIX_PATH="testpath=$HOME/testpath''${NIX_PATH:+:$NIX_PATH}"
export PATH="''${PATH:+$PATH:}$HOME/bin"
export XDG_CACHE_HOME="/home/hm-user/.cache"
export XDG_CONFIG_HOME="/home/hm-user/.config"
export XDG_DATA_HOME="/home/hm-user/.local/share"
'';
expected = pkgs.writeText "expected"
(if isDarwin then darwinExpected else linuxExpected);
in {
config = {
home.sessionVariables = lib.mkMerge [
{
PATH = "$PATH";
NIX_PATH = "$NIX_PATH";
}
{
PATH = lib.mkAfter "$HOME/bin";
NIX_PATH = lib.mkBefore "testpath=$HOME/testpath";
}
];
nmt.script = ''
assertFileExists home-path/etc/profile.d/hm-session-vars.sh
assertFileContent home-path/etc/profile.d/hm-session-vars.sh \
${expected}
'';
};
}

View file

@ -10,6 +10,8 @@ let
export __HM_SESS_VARS_SOURCED=1 export __HM_SESS_VARS_SOURCED=1
export LOCALE_ARCHIVE_2_27="${pkgs.glibcLocales}/lib/locale/locale-archive" export LOCALE_ARCHIVE_2_27="${pkgs.glibcLocales}/lib/locale/locale-archive"
export NIX_PATH="$NIX_PATH"
export PATH="$PATH"
export V1="v1" export V1="v1"
export V2="v2-v1" export V2="v2-v1"
export XDG_CACHE_HOME="/home/hm-user/.cache" export XDG_CACHE_HOME="/home/hm-user/.cache"
@ -22,6 +24,8 @@ let
if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi
export __HM_SESS_VARS_SOURCED=1 export __HM_SESS_VARS_SOURCED=1
export NIX_PATH="$NIX_PATH"
export PATH="$PATH"
export V1="v1" export V1="v1"
export V2="v2-v1" export V2="v2-v1"
export XDG_CACHE_HOME="/home/hm-user/.cache" export XDG_CACHE_HOME="/home/hm-user/.cache"