2021-11-20 18:34:13 +01:00
|
|
|
{ lib, config, pkgs }: with lib;
|
2012-10-12 23:01:49 +02:00
|
|
|
|
2019-09-08 13:31:18 +02:00
|
|
|
rec {
|
2012-10-12 23:01:49 +02:00
|
|
|
|
2020-08-16 14:34:26 +02:00
|
|
|
# Copy configuration files to avoid having the entire sources in the system closure
|
2021-08-15 17:12:23 +02:00
|
|
|
copyFile = filePath: pkgs.runCommand (builtins.unsafeDiscardStringContext (builtins.baseNameOf filePath)) {} ''
|
2020-08-16 14:34:26 +02:00
|
|
|
cp ${filePath} $out
|
|
|
|
'';
|
|
|
|
|
2020-07-27 02:05:21 +02:00
|
|
|
# Check whenever fileSystem is needed for boot. NOTE: Make sure
|
|
|
|
# pathsNeededForBoot is closed under the parent relationship, i.e. if /a/b/c
|
|
|
|
# is in the list, put /a and /a/b in as well.
|
2021-11-15 15:28:22 +01:00
|
|
|
pathsNeededForBoot = [ "/" "/nix" "/nix/store" "/var" "/var/log" "/var/lib" "/var/lib/nixos" "/etc" "/usr" ];
|
2020-07-27 02:05:21 +02:00
|
|
|
fsNeededForBoot = fs: fs.neededForBoot || elem fs.mountPoint pathsNeededForBoot;
|
2016-08-23 20:01:35 +02:00
|
|
|
|
2015-11-25 20:09:09 +01:00
|
|
|
# Check whenever `b` depends on `a` as a fileSystem
|
2020-05-21 09:10:47 +02:00
|
|
|
fsBefore = a: b:
|
|
|
|
let
|
|
|
|
# normalisePath adds a slash at the end of the path if it didn't already
|
|
|
|
# have one.
|
|
|
|
#
|
|
|
|
# The reason slashes are added at the end of each path is to prevent `b`
|
|
|
|
# from accidentally depending on `a` in cases like
|
|
|
|
# a = { mountPoint = "/aaa"; ... }
|
|
|
|
# b = { device = "/aaaa"; ... }
|
|
|
|
# Here a.mountPoint *is* a prefix of b.device even though a.mountPoint is
|
|
|
|
# *not* a parent of b.device. If we add a slash at the end of each string,
|
|
|
|
# though, this is not a problem: "/aaa/" is not a prefix of "/aaaa/".
|
|
|
|
normalisePath = path: "${path}${optionalString (!(hasSuffix "/" path)) "/"}";
|
2021-06-18 08:32:45 +02:00
|
|
|
normalise = mount: mount // { device = normalisePath (toString mount.device);
|
2020-05-21 09:10:47 +02:00
|
|
|
mountPoint = normalisePath mount.mountPoint;
|
|
|
|
depends = map normalisePath mount.depends;
|
|
|
|
};
|
|
|
|
|
|
|
|
a' = normalise a;
|
|
|
|
b' = normalise b;
|
|
|
|
|
|
|
|
in hasPrefix a'.mountPoint b'.device
|
|
|
|
|| hasPrefix a'.mountPoint b'.mountPoint
|
|
|
|
|| any (hasPrefix a'.mountPoint) b'.depends;
|
2015-11-25 20:09:09 +01:00
|
|
|
|
2012-10-12 23:01:49 +02:00
|
|
|
# Escape a path according to the systemd rules, e.g. /dev/xyzzy
|
|
|
|
# becomes dev-xyzzy. FIXME: slow.
|
|
|
|
escapeSystemdPath = s:
|
2014-09-04 03:40:05 +02:00
|
|
|
replaceChars ["/" "-" " "] ["-" "\\x2d" "\\x20"]
|
2020-03-14 04:13:04 +01:00
|
|
|
(removePrefix "/" s);
|
2012-10-12 23:01:49 +02:00
|
|
|
|
2022-01-09 08:46:55 +01:00
|
|
|
# Quotes an argument for use in Exec* service lines.
|
|
|
|
# systemd accepts "-quoted strings with escape sequences, toJSON produces
|
|
|
|
# a subset of these.
|
|
|
|
# Additionally we escape % to disallow expansion of % specifiers. Any lone ;
|
|
|
|
# in the input will be turned it ";" and thus lose its special meaning.
|
|
|
|
# Every $ is escaped to $$, this makes it unnecessary to disable environment
|
|
|
|
# substitution for the directive.
|
|
|
|
escapeSystemdExecArg = arg:
|
|
|
|
let
|
|
|
|
s = if builtins.isPath arg then "${arg}"
|
|
|
|
else if builtins.isString arg then arg
|
|
|
|
else if builtins.isInt arg || builtins.isFloat arg then toString arg
|
|
|
|
else throw "escapeSystemdExecArg only allows strings, paths and numbers";
|
|
|
|
in
|
|
|
|
replaceChars [ "%" "$" ] [ "%%" "$$" ] (builtins.toJSON s);
|
|
|
|
|
|
|
|
# Quotes a list of arguments into a single string for use in a Exec*
|
|
|
|
# line.
|
|
|
|
escapeSystemdExecArgs = concatMapStringsSep " " escapeSystemdExecArg;
|
|
|
|
|
2016-06-12 21:03:14 +02:00
|
|
|
# Returns a system path for a given shell package
|
|
|
|
toShellPath = shell:
|
|
|
|
if types.shellPackage.check shell then
|
|
|
|
"/run/current-system/sw${shell.shellPath}"
|
2016-07-04 16:10:51 +02:00
|
|
|
else if types.package.check shell then
|
|
|
|
throw "${shell} is not a shell package"
|
2016-06-12 21:03:14 +02:00
|
|
|
else
|
|
|
|
shell;
|
2019-09-06 16:38:41 +02:00
|
|
|
|
|
|
|
/* Recurse into a list or an attrset, searching for attrs named like
|
|
|
|
the value of the "attr" parameter, and return an attrset where the
|
|
|
|
names are the corresponding jq path where the attrs were found and
|
|
|
|
the values are the values of the attrs.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
recursiveGetAttrWithJqPrefix {
|
|
|
|
example = [
|
|
|
|
{
|
|
|
|
irrelevant = "not interesting";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
ignored = "ignored attr";
|
|
|
|
relevant = {
|
|
|
|
secret = {
|
|
|
|
_secret = "/path/to/secret";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
|
|
|
} "_secret" -> { ".example[1].relevant.secret" = "/path/to/secret"; }
|
|
|
|
*/
|
|
|
|
recursiveGetAttrWithJqPrefix = item: attr:
|
|
|
|
let
|
|
|
|
recurse = prefix: item:
|
|
|
|
if item ? ${attr} then
|
|
|
|
nameValuePair prefix item.${attr}
|
|
|
|
else if isAttrs item then
|
|
|
|
map (name: recurse (prefix + "." + name) item.${name}) (attrNames item)
|
|
|
|
else if isList item then
|
|
|
|
imap0 (index: item: recurse (prefix + "[${toString index}]") item) item
|
|
|
|
else
|
|
|
|
[];
|
|
|
|
in listToAttrs (flatten (recurse "" item));
|
|
|
|
|
|
|
|
/* Takes an attrset and a file path and generates a bash snippet that
|
|
|
|
outputs a JSON file at the file path with all instances of
|
|
|
|
|
|
|
|
{ _secret = "/path/to/secret" }
|
|
|
|
|
|
|
|
in the attrset replaced with the contents of the file
|
|
|
|
"/path/to/secret" in the output JSON.
|
|
|
|
|
|
|
|
When a configuration option accepts an attrset that is finally
|
|
|
|
converted to JSON, this makes it possible to let the user define
|
|
|
|
arbitrary secret values.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
If the file "/path/to/secret" contains the string
|
|
|
|
"topsecretpassword1234",
|
|
|
|
|
|
|
|
genJqSecretsReplacementSnippet {
|
|
|
|
example = [
|
|
|
|
{
|
|
|
|
irrelevant = "not interesting";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
ignored = "ignored attr";
|
|
|
|
relevant = {
|
|
|
|
secret = {
|
|
|
|
_secret = "/path/to/secret";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
|
|
|
} "/path/to/output.json"
|
|
|
|
|
|
|
|
would generate a snippet that, when run, outputs the following
|
|
|
|
JSON file at "/path/to/output.json":
|
|
|
|
|
|
|
|
{
|
|
|
|
"example": [
|
|
|
|
{
|
|
|
|
"irrelevant": "not interesting"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ignored": "ignored attr",
|
|
|
|
"relevant": {
|
|
|
|
"secret": "topsecretpassword1234"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
genJqSecretsReplacementSnippet = genJqSecretsReplacementSnippet' "_secret";
|
|
|
|
|
|
|
|
# Like genJqSecretsReplacementSnippet, but allows the name of the
|
|
|
|
# attr which identifies the secret to be changed.
|
|
|
|
genJqSecretsReplacementSnippet' = attr: set: output:
|
|
|
|
let
|
|
|
|
secrets = recursiveGetAttrWithJqPrefix set attr;
|
|
|
|
in ''
|
|
|
|
if [[ -h '${output}' ]]; then
|
|
|
|
rm '${output}'
|
|
|
|
fi
|
2022-01-31 12:44:54 +01:00
|
|
|
|
2022-02-23 12:48:51 +01:00
|
|
|
inherit_errexit_enabled=0
|
|
|
|
shopt -pq inherit_errexit && inherit_errexit_enabled=1
|
2022-01-31 12:44:54 +01:00
|
|
|
shopt -s inherit_errexit
|
2019-09-06 16:38:41 +02:00
|
|
|
''
|
|
|
|
+ concatStringsSep
|
|
|
|
"\n"
|
2022-01-31 12:44:54 +01:00
|
|
|
(imap1 (index: name: ''
|
|
|
|
secret${toString index}=$(<'${secrets.${name}}')
|
|
|
|
export secret${toString index}
|
|
|
|
'')
|
2019-09-06 16:38:41 +02:00
|
|
|
(attrNames secrets))
|
|
|
|
+ "\n"
|
|
|
|
+ "${pkgs.jq}/bin/jq >'${output}' '"
|
|
|
|
+ concatStringsSep
|
|
|
|
" | "
|
|
|
|
(imap1 (index: name: ''${name} = $ENV.secret${toString index}'')
|
|
|
|
(attrNames secrets))
|
|
|
|
+ ''
|
|
|
|
' <<'EOF'
|
|
|
|
${builtins.toJSON set}
|
|
|
|
EOF
|
2022-02-23 12:48:51 +01:00
|
|
|
(( ! $inherit_errexit_enabled )) && shopt -u inherit_errexit
|
2019-09-06 16:38:41 +02:00
|
|
|
'';
|
2021-11-20 18:34:13 +01:00
|
|
|
|
|
|
|
systemdUtils = {
|
|
|
|
lib = import ./systemd-lib.nix { inherit lib config pkgs; };
|
|
|
|
unitOptions = import ./systemd-unit-options.nix { inherit lib systemdUtils; };
|
2022-03-19 09:02:39 +01:00
|
|
|
types = import ./systemd-types.nix { inherit lib systemdUtils; };
|
2021-11-20 18:34:13 +01:00
|
|
|
};
|
2012-10-12 23:01:49 +02:00
|
|
|
}
|