Skip to content

Commit

Permalink
Merge pull request #16 from nezia1/implement-environment-variables
Browse files Browse the repository at this point in the history
modules/nixos: implement session variables
  • Loading branch information
eclairevoyant authored Feb 22, 2025
2 parents f57c321 + 5a659b1 commit 8291092
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion modules/common.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
lib,
...
}: let
inherit (lib.attrsets) mapAttrsToList;
inherit (lib.lists) isList;
inherit (lib.strings) concatStringsSep;
inherit (lib.modules) mkIf mkDefault mkDerivedConfig;
inherit (lib.options) mkOption mkEnableOption literalExpression;
inherit (lib.strings) hasPrefix;
inherit (lib.types) bool submodule str path attrsOf nullOr lines listOf package;
inherit (lib.types) bool submodule str path attrsOf nullOr lines listOf package oneOf int;

clobberOpt = config.clobberFiles;

Expand Down Expand Up @@ -99,6 +102,18 @@
}));
};
});

toEnv = env:
if isList env
then concatStringsSep ":" (map toString env)
else toString env;

mkEnvVars = vars: (concatStringsSep "\n"
(mapAttrsToList (name: value: "export ${name}=\"${toEnv value}\"") vars));

writeEnvScript = attrs:
pkgs.writeShellScript "load-env"
(mkEnvVars attrs);
in {
imports = [
# Makes "assertions" option available without having to duplicate the work
Expand Down Expand Up @@ -147,9 +162,37 @@ in {
example = literalExpression "[pkgs.hello]";
description = "Packages for ${config.user}";
};

environment = {
loadEnv = mkOption {
type = path;
readOnly = true;
description = ''
A POSIX compliant shell script containing the user session variables needed to bootstrap the session.
As there is no reliable and agnostic way of setting session variables, Hjem's
environment module does nothing by itself. Rather, it provides a POSIX compliant shell script
that needs to be sourced where needed.
'';
};
sessionVariables = mkOption {
type = attrsOf (oneOf [(listOf (oneOf [int str path])) int str path]);
default = {};
example = {
EDITOR = "nvim";
VISUAL = "nvim";
};
description = ''
A set of environment variables used in the user environment.
If a list of strings is used, they will be concatenated with colon
characters.
'';
};
};
};

config = {
environment.loadEnv = mkIf (config.environment.sessionVariables != {}) (writeEnvScript config.environment.sessionVariables);
assertions = [
{
assertion = config.user != "";
Expand Down

0 comments on commit 8291092

Please sign in to comment.