-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathenv.nix
More file actions
158 lines (145 loc) · 3.7 KB
/
env.nix
File metadata and controls
158 lines (145 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
{
config,
lib,
pkgs,
...
}:
let
inherit (lib)
mkOption
mkEnableOption
types
assertMsg
;
envOptions = {
name = mkOption {
type = types.str;
description = "Name of the environment variable";
};
value = mkOption {
type =
with types;
nullOr (oneOf [
str
int
bool
package
]);
default = null;
description = "Shell-escaped value to set";
};
eval = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Like value but not evaluated by Bash. This allows to inject other
variable names or even commands using the `$()` notation.
'';
example = "$OTHER_VAR";
};
prefix = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Prepend to PATH-like environment variables.
For example name = "PATH"; prefix = "bin"; will expand the path of
./bin and prepend it to the PATH, separated by ':'.
'';
example = "bin";
};
unset = mkEnableOption "unsets the variable";
__toString = mkOption {
type = types.functionTo types.str;
internal = true;
readOnly = true;
default = envToBash;
description = "Function used to translate this submodule to Bash code";
};
};
envToBash =
{
name,
value,
eval,
prefix,
unset,
...
}@args:
let
vals = lib.filter (key: args.${key} != null && args.${key} != false) [
"eval"
"prefix"
"unset"
"value"
];
valType = lib.head vals;
in
assert assertMsg (
(lib.length vals) > 0
) "[[environ]]: ${name} expected one of (value|eval|prefix|unset) to be set.";
assert assertMsg ((lib.length vals) < 2)
"[[environ]]: ${name} expected only one of (value|eval|prefix|unset) to be set. Not ${toString vals}";
assert assertMsg (
!(name == "PATH" && valType == "value")
) "[[environ]]: ${name} should not override the value. Use 'prefix' instead.";
if valType == "value" then
"export ${name}=${lib.escapeShellArg (toString value)}"
else if valType == "eval" then
"export ${name}=${eval}"
else if valType == "prefix" then
''export ${name}=$(${pkgs.coreutils}/bin/realpath --canonicalize-missing "${prefix}")''${${name}+:''${${name}}}''
else if valType == "unset" then
"unset ${name}"
else
throw "BUG in the env.nix module. This should never be reached.";
in
{
options.env = mkOption {
type = types.listOf (types.submodule { options = envOptions; });
default = [ ];
description = ''
Add environment variables to the shell.
'';
example = lib.literalExpression ''
[
{
name = "HTTP_PORT";
value = 8080;
}
{
name = "PATH";
prefix = "bin";
}
{
name = "XDG_CACHE_DIR";
eval = "$PRJ_ROOT/.cache";
}
{
name = "CARGO_HOME";
unset = true;
}
]
'';
};
config = {
# Default env
env = lib.mkBefore [
# Expose the path to nixpkgs
{
name = "NIXPKGS_PATH";
value = toString pkgs.path;
}
# This is used by bash-completions to find new completions on demand
{
name = "XDG_DATA_DIRS";
eval = "$DEVSHELL_DIR/share:\${XDG_DATA_DIRS:-/usr/local/share:/usr/share}";
}
# A per-project data directory for runtime information.
{
name = "PRJ_DATA_DIR";
eval = "\${PRJ_DATA_DIR:-$PRJ_ROOT/.data}";
}
];
devshell.startup_env = lib.concatStringsSep "\n" config.env;
};
}