From fa8692f84c705edc02b1145d11362f8d6809a0b4 Mon Sep 17 00:00:00 2001 From: Robert Helgesson Date: Sun, 31 Jan 2021 20:39:51 +0100 Subject: [PATCH] home-environment: make sessionVariables a freeform module Specifically, introduce explicit options for `PATH` and `NIX_PATH`. --- modules/home-environment.nix | 25 +++++++- tests/modules/home-environment/default.nix | 3 +- .../session-variables-explicit.nix | 57 +++++++++++++++++++ .../home-environment/session-variables.nix | 4 ++ 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 tests/modules/home-environment/session-variables-explicit.nix diff --git a/modules/home-environment.nix b/modules/home-environment.nix index e5b6cc3a6..85d18f63a 100644 --- a/modules/home-environment.nix +++ b/modules/home-environment.nix @@ -229,7 +229,30 @@ in home.sessionVariables = mkOption { 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 PATH variable. + ''; + }; + + NIX_PATH = mkOption { + type = types.envVar; + default = "$NIX_PATH"; + defaultText = literalExample ''"$NIX_PATH"''; + description = '' + Content of the NIX_PATH variable. + ''; + }; + }; + }; example = { EDITOR = "emacs"; GS_OPTIONS = "-sPAPERSIZE=a4"; }; description = '' Environment variables to always set at login. diff --git a/tests/modules/home-environment/default.nix b/tests/modules/home-environment/default.nix index e76e248a1..9fcafdcd0 100644 --- a/tests/modules/home-environment/default.nix +++ b/tests/modules/home-environment/default.nix @@ -1,4 +1,5 @@ { - home-session-variables = ./session-variables.nix; home-session-path = ./session-path.nix; + home-session-variables = ./session-variables.nix; + home-session-variables-explicit = ./session-variables-explicit.nix; } diff --git a/tests/modules/home-environment/session-variables-explicit.nix b/tests/modules/home-environment/session-variables-explicit.nix new file mode 100644 index 000000000..8ee540716 --- /dev/null +++ b/tests/modules/home-environment/session-variables-explicit.nix @@ -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" + export 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" + export 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} + ''; + }; +} diff --git a/tests/modules/home-environment/session-variables.nix b/tests/modules/home-environment/session-variables.nix index d939c05eb..bafedf9f2 100644 --- a/tests/modules/home-environment/session-variables.nix +++ b/tests/modules/home-environment/session-variables.nix @@ -10,6 +10,8 @@ let export __HM_SESS_VARS_SOURCED=1 export LOCALE_ARCHIVE_2_27="${pkgs.glibcLocales}/lib/locale/locale-archive" + export NIX_PATH="$NIX_PATH" + export PATH="$PATH" export V1="v1" export V2="v2-v1" export XDG_CACHE_HOME="/home/hm-user/.cache" @@ -22,6 +24,8 @@ let if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi export __HM_SESS_VARS_SOURCED=1 + export NIX_PATH="$NIX_PATH" + export PATH="$PATH" export V1="v1" export V2="v2-v1" export XDG_CACHE_HOME="/home/hm-user/.cache"