2024-03-21 13:52:12 +00:00
|
|
|
|
{ config, lib, pkgs, utils }:
|
2015-04-20 09:31:17 +00:00
|
|
|
|
|
2018-01-03 10:57:29 +00:00
|
|
|
|
let
|
2024-03-28 04:42:17 +00:00
|
|
|
|
inherit (lib)
|
|
|
|
|
all
|
|
|
|
|
attrByPath
|
|
|
|
|
attrNames
|
|
|
|
|
concatLists
|
|
|
|
|
concatMap
|
|
|
|
|
concatMapStrings
|
|
|
|
|
concatStrings
|
|
|
|
|
concatStringsSep
|
|
|
|
|
const
|
|
|
|
|
elem
|
2024-07-22 21:51:18 +00:00
|
|
|
|
elemAt
|
2024-03-28 04:42:17 +00:00
|
|
|
|
filter
|
|
|
|
|
filterAttrs
|
2024-03-25 18:30:46 +00:00
|
|
|
|
flatten
|
2024-03-28 04:42:17 +00:00
|
|
|
|
flip
|
2024-10-06 21:52:42 +00:00
|
|
|
|
hasPrefix
|
2024-03-28 04:42:17 +00:00
|
|
|
|
head
|
|
|
|
|
isInt
|
2024-05-06 17:32:34 +00:00
|
|
|
|
isFloat
|
2024-03-28 04:42:17 +00:00
|
|
|
|
isList
|
2024-03-25 18:30:46 +00:00
|
|
|
|
isPath
|
2024-07-22 21:51:18 +00:00
|
|
|
|
isString
|
2024-03-28 04:42:17 +00:00
|
|
|
|
length
|
|
|
|
|
makeBinPath
|
|
|
|
|
makeSearchPathOutput
|
|
|
|
|
mapAttrs
|
|
|
|
|
mapAttrsToList
|
2024-07-22 21:51:18 +00:00
|
|
|
|
mapNullable
|
|
|
|
|
match
|
2024-03-28 04:42:17 +00:00
|
|
|
|
mkAfter
|
|
|
|
|
mkIf
|
|
|
|
|
optional
|
|
|
|
|
optionalAttrs
|
|
|
|
|
optionalString
|
2024-03-25 18:30:46 +00:00
|
|
|
|
pipe
|
2024-03-28 04:42:17 +00:00
|
|
|
|
range
|
|
|
|
|
replaceStrings
|
|
|
|
|
reverseList
|
|
|
|
|
splitString
|
|
|
|
|
stringLength
|
|
|
|
|
stringToCharacters
|
|
|
|
|
tail
|
|
|
|
|
toIntBase10
|
|
|
|
|
trace
|
|
|
|
|
types
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
inherit (lib.strings) toJSON;
|
|
|
|
|
|
2018-01-03 10:57:29 +00:00
|
|
|
|
cfg = config.systemd;
|
2021-10-21 01:55:36 +00:00
|
|
|
|
lndir = "${pkgs.buildPackages.xorg.lndir}/bin/lndir";
|
2022-03-13 15:09:36 +00:00
|
|
|
|
systemd = cfg.package;
|
2018-01-03 10:57:29 +00:00
|
|
|
|
in rec {
|
2015-04-20 09:31:17 +00:00
|
|
|
|
|
2022-12-12 01:36:03 +00:00
|
|
|
|
shellEscape = s: (replaceStrings [ "\\" ] [ "\\\\" ] s);
|
2015-04-20 09:31:17 +00:00
|
|
|
|
|
2024-03-28 04:42:17 +00:00
|
|
|
|
mkPathSafeName = replaceStrings ["@" ":" "\\" "[" "]"] ["-" "-" "-" "" ""];
|
nixos: Add 'chroot' options to systemd.services
Currently, if you want to properly chroot a systemd service, you could
do it using BindReadOnlyPaths=/nix/store (which is not what I'd call
"properly", because the whole store is still accessible) or use a
separate derivation that gathers the runtime closure of the service you
want to chroot. The former is the easier method and there is also a
method directly offered by systemd, called ProtectSystem, which still
leaves the whole store accessible. The latter however is a bit more
involved, because you need to bind-mount each store path of the runtime
closure of the service you want to chroot.
This can be achieved using pkgs.closureInfo and a small derivation that
packs everything into a systemd unit, which later can be added to
systemd.packages. That's also what I did several times[1][2] in the
past.
However, this process got a bit tedious, so I decided that it would be
generally useful for NixOS, so this very implementation was born.
Now if you want to chroot a systemd service, all you need to do is:
{
systemd.services.yourservice = {
description = "My Shiny Service";
wantedBy = [ "multi-user.target" ];
chroot.enable = true;
serviceConfig.ExecStart = "${pkgs.myservice}/bin/myservice";
};
}
If more than the dependencies for the ExecStart* and ExecStop* (which
btw. also includes "script" and {pre,post}Start) need to be in the
chroot, it can be specified using the chroot.packages option. By
default (which uses the "full-apivfs"[3] confinement mode), a user
namespace is set up as well and /proc, /sys and /dev are mounted
appropriately.
In addition - and by default - a /bin/sh executable is provided as well,
which is useful for most programs that use the system() C library call
to execute commands via shell. The shell providing /bin/sh is dash
instead of the default in NixOS (which is bash), because it's way more
lightweight and after all we're chrooting because we want to lower the
attack surface and it should be only used for "/bin/sh -c something".
Prior to submitting this here, I did a first implementation of this
outside[4] of nixpkgs, which duplicated the "pathSafeName" functionality
from systemd-lib.nix, just because it's only a single line.
However, I decided to just re-use the one from systemd here and
subsequently made it available when importing systemd-lib.nix, so that
the systemd-chroot implementation also benefits from fixes to that
functionality (which is now a proper function).
Unfortunately, we do have a few limitations as well. The first being
that DynamicUser doesn't work in conjunction with tmpfs, because it
already sets up a tmpfs in a different path and simply ignores the one
we define. We could probably solve this by detecting it and try to
bind-mount our paths to that different path whenever DynamicUser is
enabled.
The second limitation/issue is that RootDirectoryStartOnly doesn't work
right now, because it only affects the RootDirectory option and not the
individual bind mounts or our tmpfs. It would be helpful if systemd
would have a way to disable specific bind mounts as well or at least
have some way to ignore failures for the bind mounts/tmpfs setup.
Another quirk we do have right now is that systemd tries to create a
/usr directory within the chroot, which subsequently fails. Fortunately,
this is just an ugly error and not a hard failure.
[1]: https://github.com/headcounter/shabitica/blob/3bb01728a0237ad5e7/default.nix#L43-L62
[2]: https://github.com/aszlig/avonc/blob/dedf29e092481a33dc/nextcloud.nix#L103-L124
[3]: The reason this is called "full-apivfs" instead of just "full" is
to make room for a *real* "full" confinement mode, which is more
restrictive even.
[4]: https://github.com/aszlig/avonc/blob/92a20bece4df54625e/systemd-chroot.nix
Signed-off-by: aszlig <aszlig@nix.build>
2019-03-10 11:21:55 +00:00
|
|
|
|
|
2021-12-27 12:00:00 +00:00
|
|
|
|
# a type for options that take a unit name
|
|
|
|
|
unitNameType = types.strMatching "[a-zA-Z0-9@%:_.\\-]+[.](service|socket|device|mount|automount|swap|target|path|timer|scope|slice)";
|
|
|
|
|
|
2015-04-20 09:31:17 +00:00
|
|
|
|
makeUnit = name: unit:
|
|
|
|
|
if unit.enable then
|
nixos: Add 'chroot' options to systemd.services
Currently, if you want to properly chroot a systemd service, you could
do it using BindReadOnlyPaths=/nix/store (which is not what I'd call
"properly", because the whole store is still accessible) or use a
separate derivation that gathers the runtime closure of the service you
want to chroot. The former is the easier method and there is also a
method directly offered by systemd, called ProtectSystem, which still
leaves the whole store accessible. The latter however is a bit more
involved, because you need to bind-mount each store path of the runtime
closure of the service you want to chroot.
This can be achieved using pkgs.closureInfo and a small derivation that
packs everything into a systemd unit, which later can be added to
systemd.packages. That's also what I did several times[1][2] in the
past.
However, this process got a bit tedious, so I decided that it would be
generally useful for NixOS, so this very implementation was born.
Now if you want to chroot a systemd service, all you need to do is:
{
systemd.services.yourservice = {
description = "My Shiny Service";
wantedBy = [ "multi-user.target" ];
chroot.enable = true;
serviceConfig.ExecStart = "${pkgs.myservice}/bin/myservice";
};
}
If more than the dependencies for the ExecStart* and ExecStop* (which
btw. also includes "script" and {pre,post}Start) need to be in the
chroot, it can be specified using the chroot.packages option. By
default (which uses the "full-apivfs"[3] confinement mode), a user
namespace is set up as well and /proc, /sys and /dev are mounted
appropriately.
In addition - and by default - a /bin/sh executable is provided as well,
which is useful for most programs that use the system() C library call
to execute commands via shell. The shell providing /bin/sh is dash
instead of the default in NixOS (which is bash), because it's way more
lightweight and after all we're chrooting because we want to lower the
attack surface and it should be only used for "/bin/sh -c something".
Prior to submitting this here, I did a first implementation of this
outside[4] of nixpkgs, which duplicated the "pathSafeName" functionality
from systemd-lib.nix, just because it's only a single line.
However, I decided to just re-use the one from systemd here and
subsequently made it available when importing systemd-lib.nix, so that
the systemd-chroot implementation also benefits from fixes to that
functionality (which is now a proper function).
Unfortunately, we do have a few limitations as well. The first being
that DynamicUser doesn't work in conjunction with tmpfs, because it
already sets up a tmpfs in a different path and simply ignores the one
we define. We could probably solve this by detecting it and try to
bind-mount our paths to that different path whenever DynamicUser is
enabled.
The second limitation/issue is that RootDirectoryStartOnly doesn't work
right now, because it only affects the RootDirectory option and not the
individual bind mounts or our tmpfs. It would be helpful if systemd
would have a way to disable specific bind mounts as well or at least
have some way to ignore failures for the bind mounts/tmpfs setup.
Another quirk we do have right now is that systemd tries to create a
/usr directory within the chroot, which subsequently fails. Fortunately,
this is just an ugly error and not a hard failure.
[1]: https://github.com/headcounter/shabitica/blob/3bb01728a0237ad5e7/default.nix#L43-L62
[2]: https://github.com/aszlig/avonc/blob/dedf29e092481a33dc/nextcloud.nix#L103-L124
[3]: The reason this is called "full-apivfs" instead of just "full" is
to make room for a *real* "full" confinement mode, which is more
restrictive even.
[4]: https://github.com/aszlig/avonc/blob/92a20bece4df54625e/systemd-chroot.nix
Signed-off-by: aszlig <aszlig@nix.build>
2019-03-10 11:21:55 +00:00
|
|
|
|
pkgs.runCommand "unit-${mkPathSafeName name}"
|
2015-07-07 13:01:36 +00:00
|
|
|
|
{ preferLocalBuild = true;
|
|
|
|
|
allowSubstitutes = false;
|
2023-11-01 13:16:59 +00:00
|
|
|
|
# unit.text can be null. But variables that are null listed in
|
|
|
|
|
# passAsFile are ignored by nix, resulting in no file being created,
|
|
|
|
|
# making the mv operation fail.
|
|
|
|
|
text = optionalString (unit.text != null) unit.text;
|
2023-10-26 10:34:19 +00:00
|
|
|
|
passAsFile = [ "text" ];
|
2015-07-07 13:01:36 +00:00
|
|
|
|
}
|
2015-04-20 09:31:17 +00:00
|
|
|
|
''
|
2021-08-11 18:28:30 +00:00
|
|
|
|
name=${shellEscape name}
|
2023-03-20 00:11:23 +00:00
|
|
|
|
mkdir -p "$out/$(dirname -- "$name")"
|
2023-10-26 10:34:19 +00:00
|
|
|
|
mv "$textPath" "$out/$name"
|
2015-04-20 09:31:17 +00:00
|
|
|
|
''
|
|
|
|
|
else
|
nixos: Add 'chroot' options to systemd.services
Currently, if you want to properly chroot a systemd service, you could
do it using BindReadOnlyPaths=/nix/store (which is not what I'd call
"properly", because the whole store is still accessible) or use a
separate derivation that gathers the runtime closure of the service you
want to chroot. The former is the easier method and there is also a
method directly offered by systemd, called ProtectSystem, which still
leaves the whole store accessible. The latter however is a bit more
involved, because you need to bind-mount each store path of the runtime
closure of the service you want to chroot.
This can be achieved using pkgs.closureInfo and a small derivation that
packs everything into a systemd unit, which later can be added to
systemd.packages. That's also what I did several times[1][2] in the
past.
However, this process got a bit tedious, so I decided that it would be
generally useful for NixOS, so this very implementation was born.
Now if you want to chroot a systemd service, all you need to do is:
{
systemd.services.yourservice = {
description = "My Shiny Service";
wantedBy = [ "multi-user.target" ];
chroot.enable = true;
serviceConfig.ExecStart = "${pkgs.myservice}/bin/myservice";
};
}
If more than the dependencies for the ExecStart* and ExecStop* (which
btw. also includes "script" and {pre,post}Start) need to be in the
chroot, it can be specified using the chroot.packages option. By
default (which uses the "full-apivfs"[3] confinement mode), a user
namespace is set up as well and /proc, /sys and /dev are mounted
appropriately.
In addition - and by default - a /bin/sh executable is provided as well,
which is useful for most programs that use the system() C library call
to execute commands via shell. The shell providing /bin/sh is dash
instead of the default in NixOS (which is bash), because it's way more
lightweight and after all we're chrooting because we want to lower the
attack surface and it should be only used for "/bin/sh -c something".
Prior to submitting this here, I did a first implementation of this
outside[4] of nixpkgs, which duplicated the "pathSafeName" functionality
from systemd-lib.nix, just because it's only a single line.
However, I decided to just re-use the one from systemd here and
subsequently made it available when importing systemd-lib.nix, so that
the systemd-chroot implementation also benefits from fixes to that
functionality (which is now a proper function).
Unfortunately, we do have a few limitations as well. The first being
that DynamicUser doesn't work in conjunction with tmpfs, because it
already sets up a tmpfs in a different path and simply ignores the one
we define. We could probably solve this by detecting it and try to
bind-mount our paths to that different path whenever DynamicUser is
enabled.
The second limitation/issue is that RootDirectoryStartOnly doesn't work
right now, because it only affects the RootDirectory option and not the
individual bind mounts or our tmpfs. It would be helpful if systemd
would have a way to disable specific bind mounts as well or at least
have some way to ignore failures for the bind mounts/tmpfs setup.
Another quirk we do have right now is that systemd tries to create a
/usr directory within the chroot, which subsequently fails. Fortunately,
this is just an ugly error and not a hard failure.
[1]: https://github.com/headcounter/shabitica/blob/3bb01728a0237ad5e7/default.nix#L43-L62
[2]: https://github.com/aszlig/avonc/blob/dedf29e092481a33dc/nextcloud.nix#L103-L124
[3]: The reason this is called "full-apivfs" instead of just "full" is
to make room for a *real* "full" confinement mode, which is more
restrictive even.
[4]: https://github.com/aszlig/avonc/blob/92a20bece4df54625e/systemd-chroot.nix
Signed-off-by: aszlig <aszlig@nix.build>
2019-03-10 11:21:55 +00:00
|
|
|
|
pkgs.runCommand "unit-${mkPathSafeName name}-disabled"
|
2015-07-07 13:01:36 +00:00
|
|
|
|
{ preferLocalBuild = true;
|
|
|
|
|
allowSubstitutes = false;
|
|
|
|
|
}
|
2015-04-20 09:31:17 +00:00
|
|
|
|
''
|
2021-08-11 18:28:30 +00:00
|
|
|
|
name=${shellEscape name}
|
|
|
|
|
mkdir -p "$out/$(dirname "$name")"
|
|
|
|
|
ln -s /dev/null "$out/$name"
|
2015-04-20 09:31:17 +00:00
|
|
|
|
'';
|
|
|
|
|
|
2015-04-30 04:44:25 +00:00
|
|
|
|
boolValues = [true false "yes" "no"];
|
|
|
|
|
|
|
|
|
|
digits = map toString (range 0 9);
|
|
|
|
|
|
|
|
|
|
isByteFormat = s:
|
|
|
|
|
let
|
|
|
|
|
l = reverseList (stringToCharacters s);
|
|
|
|
|
suffix = head l;
|
|
|
|
|
nums = tail l;
|
|
|
|
|
in elem suffix (["K" "M" "G" "T"] ++ digits)
|
|
|
|
|
&& all (num: elem num digits) nums;
|
|
|
|
|
|
|
|
|
|
assertByteFormat = name: group: attr:
|
|
|
|
|
optional (attr ? ${name} && ! isByteFormat attr.${name})
|
|
|
|
|
"Systemd ${group} field `${name}' must be in byte format [0-9]+[KMGT].";
|
|
|
|
|
|
2024-07-22 21:51:18 +00:00
|
|
|
|
toIntBaseDetected = value: assert (match "[0-9]+|0x[0-9a-fA-F]+" value) != null; (builtins.fromTOML "v=${value}").v;
|
|
|
|
|
|
2015-04-30 04:44:25 +00:00
|
|
|
|
hexChars = stringToCharacters "0123456789abcdefABCDEF";
|
|
|
|
|
|
|
|
|
|
isMacAddress = s: stringLength s == 17
|
|
|
|
|
&& flip all (splitString ":" s) (bytes:
|
|
|
|
|
all (byte: elem byte hexChars) (stringToCharacters bytes)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assertMacAddress = name: group: attr:
|
|
|
|
|
optional (attr ? ${name} && ! isMacAddress attr.${name})
|
nixos/networkd: support netdev MAC addresses
According to systemd.netdev manpage:
```
MACAddress=
Specifies the MAC address to use for the device, or takes the special value "none". When "none", systemd-networkd does not request the MAC address for
the device, and the kernel will assign a random MAC address. For "tun", "tap", or "l2tp" devices, the MACAddress= setting in the [NetDev] section is
not supported and will be ignored. Please specify it in the [Link] section of the corresponding systemd.network(5) file. If this option is not set,
"vlan" device inherits the MAC address of the master interface. For other kind of netdevs, if this option is not set, then the MAC address is
generated based on the interface name and the machine-id(5).
Note, even if "none" is specified, systemd-udevd will assign the persistent MAC address for the device, as 99-default.link has
MACAddressPolicy=persistent. So, it is also necessary to create a custom .link file for the device, if the MAC address assignment is not desired.
```
Therefore, `none` is an acceptable value.
2023-07-03 23:21:35 +00:00
|
|
|
|
"Systemd ${group} field `${name}' must be a valid MAC address.";
|
|
|
|
|
|
|
|
|
|
assertNetdevMacAddress = name: group: attr:
|
2023-07-20 09:03:46 +00:00
|
|
|
|
optional (attr ? ${name} && (! isMacAddress attr.${name} && attr.${name} != "none"))
|
nixos/networkd: support netdev MAC addresses
According to systemd.netdev manpage:
```
MACAddress=
Specifies the MAC address to use for the device, or takes the special value "none". When "none", systemd-networkd does not request the MAC address for
the device, and the kernel will assign a random MAC address. For "tun", "tap", or "l2tp" devices, the MACAddress= setting in the [NetDev] section is
not supported and will be ignored. Please specify it in the [Link] section of the corresponding systemd.network(5) file. If this option is not set,
"vlan" device inherits the MAC address of the master interface. For other kind of netdevs, if this option is not set, then the MAC address is
generated based on the interface name and the machine-id(5).
Note, even if "none" is specified, systemd-udevd will assign the persistent MAC address for the device, as 99-default.link has
MACAddressPolicy=persistent. So, it is also necessary to create a custom .link file for the device, if the MAC address assignment is not desired.
```
Therefore, `none` is an acceptable value.
2023-07-03 23:21:35 +00:00
|
|
|
|
"Systemd ${group} field `${name}` must be a valid MAC address or the special value `none`.";
|
|
|
|
|
|
2024-03-06 08:11:54 +00:00
|
|
|
|
isNumberOrRangeOf = check: v:
|
|
|
|
|
if isInt v
|
|
|
|
|
then check v
|
|
|
|
|
else let
|
|
|
|
|
parts = splitString "-" v;
|
|
|
|
|
lower = toIntBase10 (head parts);
|
|
|
|
|
upper = if tail parts != [] then toIntBase10 (head (tail parts)) else lower;
|
|
|
|
|
in
|
|
|
|
|
length parts <= 2 && lower <= upper && check lower && check upper;
|
2020-02-29 17:17:27 +00:00
|
|
|
|
isPort = i: i >= 0 && i <= 65535;
|
2024-03-06 08:11:54 +00:00
|
|
|
|
isPortOrPortRange = isNumberOrRangeOf isPort;
|
2020-02-29 17:17:27 +00:00
|
|
|
|
|
|
|
|
|
assertPort = name: group: attr:
|
|
|
|
|
optional (attr ? ${name} && ! isPort attr.${name})
|
|
|
|
|
"Error on the systemd ${group} field `${name}': ${attr.name} is not a valid port number.";
|
2015-04-30 04:44:25 +00:00
|
|
|
|
|
2024-03-06 08:11:54 +00:00
|
|
|
|
assertPortOrPortRange = name: group: attr:
|
|
|
|
|
optional (attr ? ${name} && ! isPortOrPortRange attr.${name})
|
|
|
|
|
"Error on the systemd ${group} field `${name}': ${attr.name} is not a valid port number or range of port numbers.";
|
|
|
|
|
|
2015-04-30 04:44:25 +00:00
|
|
|
|
assertValueOneOf = name: values: group: attr:
|
|
|
|
|
optional (attr ? ${name} && !elem attr.${name} values)
|
2018-09-20 11:40:50 +00:00
|
|
|
|
"Systemd ${group} field `${name}' cannot have value `${toString attr.${name}}'.";
|
2015-04-30 04:44:25 +00:00
|
|
|
|
|
2023-09-28 15:04:35 +00:00
|
|
|
|
assertValuesSomeOfOr = name: values: default: group: attr:
|
|
|
|
|
optional (attr ? ${name} && !(all (x: elem x values) (splitString " " attr.${name}) || attr.${name} == default))
|
|
|
|
|
"Systemd ${group} field `${name}' cannot have value `${toString attr.${name}}'.";
|
|
|
|
|
|
2015-04-30 04:44:25 +00:00
|
|
|
|
assertHasField = name: group: attr:
|
|
|
|
|
optional (!(attr ? ${name}))
|
|
|
|
|
"Systemd ${group} field `${name}' must exist.";
|
|
|
|
|
|
|
|
|
|
assertRange = name: min: max: group: attr:
|
|
|
|
|
optional (attr ? ${name} && !(min <= attr.${name} && max >= attr.${name}))
|
|
|
|
|
"Systemd ${group} field `${name}' is outside the range [${toString min},${toString max}]";
|
|
|
|
|
|
2024-04-11 07:56:18 +00:00
|
|
|
|
assertRangeOrOneOf = name: min: max: values: group: attr:
|
2024-05-06 17:32:34 +00:00
|
|
|
|
optional (attr ? ${name} && !(((isInt attr.${name} || isFloat attr.${name}) && min <= attr.${name} && max >= attr.${name}) || elem attr.${name} values))
|
2024-04-11 07:56:18 +00:00
|
|
|
|
"Systemd ${group} field `${name}' is not a value in range [${toString min},${toString max}], or one of ${toString values}";
|
|
|
|
|
|
2024-07-22 21:51:18 +00:00
|
|
|
|
assertRangeWithOptionalMask = name: min: max: group: attr:
|
|
|
|
|
if (attr ? ${name}) then
|
|
|
|
|
if isInt attr.${name} then
|
|
|
|
|
assertRange name min max group attr
|
|
|
|
|
else if isString attr.${name} then
|
|
|
|
|
let
|
|
|
|
|
fields = match "([0-9]+|0x[0-9a-fA-F]+)(/([0-9]+|0x[0-9a-fA-F]+))?" attr.${name};
|
|
|
|
|
in if fields == null then ["Systemd ${group} field `${name}' must either be an integer or two integers separated by a slash (/)."]
|
|
|
|
|
else let
|
|
|
|
|
value = toIntBaseDetected (elemAt fields 0);
|
|
|
|
|
mask = mapNullable toIntBaseDetected (elemAt fields 2);
|
|
|
|
|
in
|
|
|
|
|
optional (!(min <= value && max >= value)) "Systemd ${group} field `${name}' has main value outside the range [${toString min},${toString max}]."
|
|
|
|
|
++ optional (mask != null && !(min <= mask && max >= mask)) "Systemd ${group} field `${name}' has mask outside the range [${toString min},${toString max}]."
|
|
|
|
|
else ["Systemd ${group} field `${name}' must either be an integer or a string."]
|
|
|
|
|
else [];
|
|
|
|
|
|
2018-08-28 22:31:45 +00:00
|
|
|
|
assertMinimum = name: min: group: attr:
|
|
|
|
|
optional (attr ? ${name} && attr.${name} < min)
|
|
|
|
|
"Systemd ${group} field `${name}' must be greater than or equal to ${toString min}";
|
|
|
|
|
|
2015-04-30 04:44:25 +00:00
|
|
|
|
assertOnlyFields = fields: group: attr:
|
|
|
|
|
let badFields = filter (name: ! elem name fields) (attrNames attr); in
|
|
|
|
|
optional (badFields != [ ])
|
|
|
|
|
"Systemd ${group} has extra fields [${concatStringsSep " " badFields}].";
|
|
|
|
|
|
2018-08-28 22:31:45 +00:00
|
|
|
|
assertInt = name: group: attr:
|
|
|
|
|
optional (attr ? ${name} && !isInt attr.${name})
|
|
|
|
|
"Systemd ${group} field `${name}' is not an integer";
|
|
|
|
|
|
2024-04-26 21:18:12 +00:00
|
|
|
|
assertRemoved = name: see: group: attr:
|
|
|
|
|
optional (attr ? ${name})
|
|
|
|
|
"Systemd ${group} field `${name}' has been removed. See ${see}";
|
|
|
|
|
|
2024-10-06 21:52:42 +00:00
|
|
|
|
assertKeyIsSystemdCredential = name: group: attr:
|
|
|
|
|
optional (attr ? ${name} && !(hasPrefix "@" attr.${name}))
|
|
|
|
|
"Systemd ${group} field `${name}' is not a systemd credential";
|
|
|
|
|
|
2018-06-04 13:34:21 +00:00
|
|
|
|
checkUnitConfig = group: checks: attrs: let
|
|
|
|
|
# We're applied at the top-level type (attrsOf unitOption), so the actual
|
2021-02-11 21:18:21 +00:00
|
|
|
|
# unit options might contain attributes from mkOverride and mkIf that we need to
|
2018-06-04 13:34:21 +00:00
|
|
|
|
# convert into single values before checking them.
|
|
|
|
|
defs = mapAttrs (const (v:
|
2021-02-11 21:18:21 +00:00
|
|
|
|
if v._type or "" == "override" then v.content
|
|
|
|
|
else if v._type or "" == "if" then v.content
|
|
|
|
|
else v
|
2018-06-04 13:34:21 +00:00
|
|
|
|
)) attrs;
|
|
|
|
|
errors = concatMap (c: c group defs) checks;
|
|
|
|
|
in if errors == [] then true
|
2024-03-28 04:42:17 +00:00
|
|
|
|
else trace (concatStringsSep "\n" errors) false;
|
2015-04-30 04:44:25 +00:00
|
|
|
|
|
2024-05-17 16:21:52 +00:00
|
|
|
|
checkUnitConfigWithLegacyKey = legacyKey: group: checks: attrs:
|
|
|
|
|
let
|
|
|
|
|
dump = lib.generators.toPretty { }
|
|
|
|
|
(lib.generators.withRecursion { depthLimit = 2; throwOnDepthLimit = false; } attrs);
|
|
|
|
|
attrs' =
|
|
|
|
|
if legacyKey == null
|
|
|
|
|
then attrs
|
|
|
|
|
else if ! attrs?${legacyKey}
|
|
|
|
|
then attrs
|
|
|
|
|
else if removeAttrs attrs [ legacyKey ] == {}
|
|
|
|
|
then attrs.${legacyKey}
|
|
|
|
|
else throw ''
|
|
|
|
|
The declaration
|
|
|
|
|
|
|
|
|
|
${dump}
|
|
|
|
|
|
|
|
|
|
must not mix unit options with the legacy key '${legacyKey}'.
|
|
|
|
|
|
|
|
|
|
This can be fixed by moving all settings from within ${legacyKey}
|
|
|
|
|
one level up.
|
|
|
|
|
'';
|
|
|
|
|
in
|
|
|
|
|
checkUnitConfig group checks attrs';
|
|
|
|
|
|
2015-04-30 04:44:25 +00:00
|
|
|
|
toOption = x:
|
|
|
|
|
if x == true then "true"
|
|
|
|
|
else if x == false then "false"
|
|
|
|
|
else toString x;
|
|
|
|
|
|
|
|
|
|
attrsToSection = as:
|
|
|
|
|
concatStrings (concatLists (mapAttrsToList (name: value:
|
|
|
|
|
map (x: ''
|
|
|
|
|
${name}=${toOption x}
|
|
|
|
|
'')
|
|
|
|
|
(if isList value then value else [value]))
|
|
|
|
|
as));
|
|
|
|
|
|
2022-03-20 02:58:38 +00:00
|
|
|
|
generateUnits = { allowCollisions ? true, type, units, upstreamUnits, upstreamWants, packages ? cfg.packages, package ? cfg.package }:
|
|
|
|
|
let
|
|
|
|
|
typeDir = ({
|
|
|
|
|
system = "system";
|
|
|
|
|
initrd = "system";
|
|
|
|
|
user = "user";
|
|
|
|
|
nspawn = "nspawn";
|
|
|
|
|
}).${type};
|
|
|
|
|
in pkgs.runCommand "${type}-units"
|
2015-07-07 13:01:36 +00:00
|
|
|
|
{ preferLocalBuild = true;
|
|
|
|
|
allowSubstitutes = false;
|
|
|
|
|
} ''
|
2015-04-20 09:31:17 +00:00
|
|
|
|
mkdir -p $out
|
|
|
|
|
|
|
|
|
|
# Copy the upstream systemd units we're interested in.
|
|
|
|
|
for i in ${toString upstreamUnits}; do
|
2022-03-20 02:58:38 +00:00
|
|
|
|
fn=${package}/example/systemd/${typeDir}/$i
|
2015-04-20 09:31:17 +00:00
|
|
|
|
if ! [ -e $fn ]; then echo "missing $fn"; false; fi
|
|
|
|
|
if [ -L $fn ]; then
|
|
|
|
|
target="$(readlink "$fn")"
|
|
|
|
|
if [ ''${target:0:3} = ../ ]; then
|
|
|
|
|
ln -s "$(readlink -f "$fn")" $out/
|
|
|
|
|
else
|
|
|
|
|
cp -pd $fn $out/
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
ln -s $fn $out/
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Copy .wants links, but only those that point to units that
|
|
|
|
|
# we're interested in.
|
|
|
|
|
for i in ${toString upstreamWants}; do
|
2022-03-20 02:58:38 +00:00
|
|
|
|
fn=${package}/example/systemd/${typeDir}/$i
|
2015-04-20 09:31:17 +00:00
|
|
|
|
if ! [ -e $fn ]; then echo "missing $fn"; false; fi
|
|
|
|
|
x=$out/$(basename $fn)
|
|
|
|
|
mkdir $x
|
|
|
|
|
for i in $fn/*; do
|
|
|
|
|
y=$x/$(basename $i)
|
|
|
|
|
cp -pd $i $y
|
|
|
|
|
if ! [ -e $y ]; then rm $y; fi
|
|
|
|
|
done
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Symlink all units provided listed in systemd.packages.
|
2022-03-20 02:58:38 +00:00
|
|
|
|
packages="${toString packages}"
|
2020-01-08 02:41:28 +00:00
|
|
|
|
|
|
|
|
|
# Filter duplicate directories
|
|
|
|
|
declare -A unique_packages
|
|
|
|
|
for k in $packages ; do unique_packages[$k]=1 ; done
|
|
|
|
|
|
|
|
|
|
for i in ''${!unique_packages[@]}; do
|
2022-03-20 02:58:38 +00:00
|
|
|
|
for fn in $i/etc/systemd/${typeDir}/* $i/lib/systemd/${typeDir}/*; do
|
2015-04-20 09:31:17 +00:00
|
|
|
|
if ! [[ "$fn" =~ .wants$ ]]; then
|
2018-01-03 10:57:29 +00:00
|
|
|
|
if [[ -d "$fn" ]]; then
|
|
|
|
|
targetDir="$out/$(basename "$fn")"
|
|
|
|
|
mkdir -p "$targetDir"
|
|
|
|
|
${lndir} "$fn" "$targetDir"
|
|
|
|
|
else
|
|
|
|
|
ln -s $fn $out/
|
|
|
|
|
fi
|
2015-04-20 09:31:17 +00:00
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
done
|
|
|
|
|
|
2022-08-06 20:29:56 +00:00
|
|
|
|
# Symlink units defined by systemd.units where override strategy
|
|
|
|
|
# shall be automatically detected. If these are also provided by
|
|
|
|
|
# systemd or systemd.packages, then add them as
|
2015-04-20 09:31:17 +00:00
|
|
|
|
# <unit-name>.d/overrides.conf, which makes them extend the
|
|
|
|
|
# upstream unit.
|
2022-08-06 20:29:56 +00:00
|
|
|
|
for i in ${toString (mapAttrsToList
|
|
|
|
|
(n: v: v.unit)
|
2024-03-28 04:42:17 +00:00
|
|
|
|
(filterAttrs (n: v: (attrByPath [ "overrideStrategy" ] "asDropinIfExists" v) == "asDropinIfExists") units))}; do
|
2015-04-20 09:31:17 +00:00
|
|
|
|
fn=$(basename $i/*)
|
2021-03-19 08:03:49 +00:00
|
|
|
|
if [ -e $out/$fn ]; then
|
2015-04-20 09:31:17 +00:00
|
|
|
|
if [ "$(readlink -f $i/$fn)" = /dev/null ]; then
|
|
|
|
|
ln -sfn /dev/null $out/$fn
|
|
|
|
|
else
|
2020-04-04 19:11:21 +00:00
|
|
|
|
${if allowCollisions then ''
|
|
|
|
|
mkdir -p $out/$fn.d
|
|
|
|
|
ln -s $i/$fn $out/$fn.d/overrides.conf
|
|
|
|
|
'' else ''
|
|
|
|
|
echo "Found multiple derivations configuring $fn!"
|
|
|
|
|
exit 1
|
|
|
|
|
''}
|
2015-04-20 09:31:17 +00:00
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
ln -fs $i/$fn $out/
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
2022-08-06 20:29:56 +00:00
|
|
|
|
# Symlink units defined by systemd.units which shall be
|
|
|
|
|
# treated as drop-in file.
|
|
|
|
|
for i in ${toString (mapAttrsToList
|
|
|
|
|
(n: v: v.unit)
|
2024-03-28 04:42:17 +00:00
|
|
|
|
(filterAttrs (n: v: v ? overrideStrategy && v.overrideStrategy == "asDropin") units))}; do
|
2022-08-06 20:29:56 +00:00
|
|
|
|
fn=$(basename $i/*)
|
|
|
|
|
mkdir -p $out/$fn.d
|
|
|
|
|
ln -s $i/$fn $out/$fn.d/overrides.conf
|
|
|
|
|
done
|
|
|
|
|
|
2017-02-01 21:51:16 +00:00
|
|
|
|
# Create service aliases from aliases option.
|
|
|
|
|
${concatStrings (mapAttrsToList (name: unit:
|
|
|
|
|
concatMapStrings (name2: ''
|
|
|
|
|
ln -sfn '${name}' $out/'${name2}'
|
2022-10-23 13:57:55 +00:00
|
|
|
|
'') (unit.aliases or [])) units)}
|
2017-02-01 21:51:16 +00:00
|
|
|
|
|
2024-02-10 11:43:31 +00:00
|
|
|
|
# Create .wants, .upholds and .requires symlinks from the wantedBy, upheldBy and
|
2015-04-20 09:31:17 +00:00
|
|
|
|
# requiredBy options.
|
|
|
|
|
${concatStrings (mapAttrsToList (name: unit:
|
|
|
|
|
concatMapStrings (name2: ''
|
|
|
|
|
mkdir -p $out/'${name2}.wants'
|
|
|
|
|
ln -sfn '../${name}' $out/'${name2}.wants'/
|
2022-10-23 13:57:55 +00:00
|
|
|
|
'') (unit.wantedBy or [])) units)}
|
2015-04-20 09:31:17 +00:00
|
|
|
|
|
2024-02-10 11:43:31 +00:00
|
|
|
|
${concatStrings (mapAttrsToList (name: unit:
|
|
|
|
|
concatMapStrings (name2: ''
|
|
|
|
|
mkdir -p $out/'${name2}.upholds'
|
|
|
|
|
ln -sfn '../${name}' $out/'${name2}.upholds'/
|
|
|
|
|
'') (unit.upheldBy or [])) units)}
|
|
|
|
|
|
2015-04-20 09:31:17 +00:00
|
|
|
|
${concatStrings (mapAttrsToList (name: unit:
|
|
|
|
|
concatMapStrings (name2: ''
|
|
|
|
|
mkdir -p $out/'${name2}.requires'
|
|
|
|
|
ln -sfn '../${name}' $out/'${name2}.requires'/
|
2022-10-23 13:57:55 +00:00
|
|
|
|
'') (unit.requiredBy or [])) units)}
|
2015-04-20 09:31:17 +00:00
|
|
|
|
|
|
|
|
|
${optionalString (type == "system") ''
|
|
|
|
|
# Stupid misc. symlinks.
|
|
|
|
|
ln -s ${cfg.defaultUnit} $out/default.target
|
2016-07-19 07:42:53 +00:00
|
|
|
|
ln -s ${cfg.ctrlAltDelUnit} $out/ctrl-alt-del.target
|
2015-04-20 09:31:17 +00:00
|
|
|
|
ln -s rescue.target $out/kbrequest.target
|
|
|
|
|
|
|
|
|
|
mkdir -p $out/getty.target.wants/
|
|
|
|
|
ln -s ../autovt@tty1.service $out/getty.target.wants/
|
|
|
|
|
|
2021-04-28 17:50:08 +00:00
|
|
|
|
ln -s ../remote-fs.target $out/multi-user.target.wants/
|
2015-04-20 09:31:17 +00:00
|
|
|
|
''}
|
|
|
|
|
''; # */
|
|
|
|
|
|
2023-11-30 09:54:09 +00:00
|
|
|
|
makeJobScript = { name, text, enableStrictShellChecks }:
|
2022-03-13 15:09:36 +00:00
|
|
|
|
let
|
2022-12-12 01:36:03 +00:00
|
|
|
|
scriptName = replaceStrings [ "\\" "@" ] [ "-" "_" ] (shellEscape name);
|
2023-11-30 09:54:09 +00:00
|
|
|
|
out = (
|
|
|
|
|
if ! enableStrictShellChecks then
|
|
|
|
|
pkgs.writeShellScriptBin scriptName ''
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
|
|
${text}
|
|
|
|
|
''
|
|
|
|
|
else
|
|
|
|
|
pkgs.writeShellApplication {
|
|
|
|
|
name = scriptName;
|
|
|
|
|
inherit text;
|
|
|
|
|
}
|
|
|
|
|
).overrideAttrs (_: {
|
2022-03-13 15:09:36 +00:00
|
|
|
|
# The derivation name is different from the script file name
|
|
|
|
|
# to keep the script file name short to avoid cluttering logs.
|
|
|
|
|
name = "unit-script-${scriptName}";
|
|
|
|
|
});
|
2023-11-30 09:54:09 +00:00
|
|
|
|
in lib.getExe out;
|
2022-03-13 15:09:36 +00:00
|
|
|
|
|
2023-09-13 19:03:37 +00:00
|
|
|
|
unitConfig = { config, name, options, ... }: {
|
2022-03-13 15:09:36 +00:00
|
|
|
|
config = {
|
|
|
|
|
unitConfig =
|
|
|
|
|
optionalAttrs (config.requires != [])
|
|
|
|
|
{ Requires = toString config.requires; }
|
|
|
|
|
// optionalAttrs (config.wants != [])
|
|
|
|
|
{ Wants = toString config.wants; }
|
2024-02-10 11:43:31 +00:00
|
|
|
|
// optionalAttrs (config.upholds != [])
|
|
|
|
|
{ Upholds = toString config.upholds; }
|
2022-03-13 15:09:36 +00:00
|
|
|
|
// optionalAttrs (config.after != [])
|
|
|
|
|
{ After = toString config.after; }
|
|
|
|
|
// optionalAttrs (config.before != [])
|
|
|
|
|
{ Before = toString config.before; }
|
|
|
|
|
// optionalAttrs (config.bindsTo != [])
|
|
|
|
|
{ BindsTo = toString config.bindsTo; }
|
|
|
|
|
// optionalAttrs (config.partOf != [])
|
|
|
|
|
{ PartOf = toString config.partOf; }
|
|
|
|
|
// optionalAttrs (config.conflicts != [])
|
|
|
|
|
{ Conflicts = toString config.conflicts; }
|
|
|
|
|
// optionalAttrs (config.requisite != [])
|
|
|
|
|
{ Requisite = toString config.requisite; }
|
2022-04-01 08:44:08 +00:00
|
|
|
|
// optionalAttrs (config ? restartTriggers && config.restartTriggers != [])
|
2024-03-25 18:30:46 +00:00
|
|
|
|
{ X-Restart-Triggers = "${pkgs.writeText "X-Restart-Triggers-${name}" (pipe config.restartTriggers [
|
|
|
|
|
flatten
|
|
|
|
|
(map (x: if isPath x then "${x}" else x))
|
|
|
|
|
toString
|
|
|
|
|
])}"; }
|
2022-04-01 08:44:08 +00:00
|
|
|
|
// optionalAttrs (config ? reloadTriggers && config.reloadTriggers != [])
|
2024-03-25 18:30:46 +00:00
|
|
|
|
{ X-Reload-Triggers = "${pkgs.writeText "X-Reload-Triggers-${name}" (pipe config.reloadTriggers [
|
|
|
|
|
flatten
|
|
|
|
|
(map (x: if isPath x then "${x}" else x))
|
|
|
|
|
toString
|
|
|
|
|
])}"; }
|
2022-03-13 15:09:36 +00:00
|
|
|
|
// optionalAttrs (config.description != "") {
|
|
|
|
|
Description = config.description; }
|
|
|
|
|
// optionalAttrs (config.documentation != []) {
|
|
|
|
|
Documentation = toString config.documentation; }
|
|
|
|
|
// optionalAttrs (config.onFailure != []) {
|
|
|
|
|
OnFailure = toString config.onFailure; }
|
2022-04-07 11:21:58 +00:00
|
|
|
|
// optionalAttrs (config.onSuccess != []) {
|
|
|
|
|
OnSuccess = toString config.onSuccess; }
|
2022-03-13 15:09:36 +00:00
|
|
|
|
// optionalAttrs (options.startLimitIntervalSec.isDefined) {
|
|
|
|
|
StartLimitIntervalSec = toString config.startLimitIntervalSec;
|
|
|
|
|
} // optionalAttrs (options.startLimitBurst.isDefined) {
|
|
|
|
|
StartLimitBurst = toString config.startLimitBurst;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-30 09:54:09 +00:00
|
|
|
|
serviceConfig =
|
|
|
|
|
let
|
|
|
|
|
nixosConfig = config;
|
|
|
|
|
in
|
|
|
|
|
{ name, lib, config, ... }: {
|
2024-03-21 13:52:12 +00:00
|
|
|
|
config = {
|
|
|
|
|
name = "${name}.service";
|
|
|
|
|
environment.PATH = mkIf (config.path != []) "${makeBinPath config.path}:${makeSearchPathOutput "bin" "sbin" config.path}";
|
2023-11-30 09:54:09 +00:00
|
|
|
|
|
|
|
|
|
enableStrictShellChecks = lib.mkOptionDefault nixosConfig.systemd.enableStrictShellChecks;
|
2024-03-21 13:52:12 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pathConfig = { name, config, ... }: {
|
|
|
|
|
config = {
|
|
|
|
|
name = "${name}.path";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
socketConfig = { name, config, ... }: {
|
|
|
|
|
config = {
|
|
|
|
|
name = "${name}.socket";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sliceConfig = { name, config, ... }: {
|
|
|
|
|
config = {
|
|
|
|
|
name = "${name}.slice";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
targetConfig = { name, config, ... }: {
|
|
|
|
|
config = {
|
|
|
|
|
name = "${name}.target";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
timerConfig = { name, config, ... }: {
|
|
|
|
|
config = {
|
|
|
|
|
name = "${name}.timer";
|
|
|
|
|
};
|
2022-03-13 15:09:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-04-01 07:39:56 +00:00
|
|
|
|
stage2ServiceConfig = {
|
|
|
|
|
imports = [ serviceConfig ];
|
|
|
|
|
# Default path for systemd services. Should be quite minimal.
|
|
|
|
|
config.path = mkAfter [
|
|
|
|
|
pkgs.coreutils
|
|
|
|
|
pkgs.findutils
|
|
|
|
|
pkgs.gnugrep
|
|
|
|
|
pkgs.gnused
|
|
|
|
|
systemd
|
|
|
|
|
];
|
|
|
|
|
};
|
2022-03-20 03:00:06 +00:00
|
|
|
|
|
2022-04-01 07:39:56 +00:00
|
|
|
|
stage1ServiceConfig = serviceConfig;
|
2022-03-20 03:00:06 +00:00
|
|
|
|
|
2022-03-13 15:09:36 +00:00
|
|
|
|
mountConfig = { config, ... }: {
|
|
|
|
|
config = {
|
2024-03-21 13:52:12 +00:00
|
|
|
|
name = "${utils.escapeSystemdPath config.where}.mount";
|
2022-03-13 15:09:36 +00:00
|
|
|
|
mountConfig =
|
|
|
|
|
{ What = config.what;
|
|
|
|
|
Where = config.where;
|
|
|
|
|
} // optionalAttrs (config.type != "") {
|
|
|
|
|
Type = config.type;
|
|
|
|
|
} // optionalAttrs (config.options != "") {
|
|
|
|
|
Options = config.options;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
automountConfig = { config, ... }: {
|
|
|
|
|
config = {
|
2024-03-21 13:52:12 +00:00
|
|
|
|
name = "${utils.escapeSystemdPath config.where}.automount";
|
2022-03-13 15:09:36 +00:00
|
|
|
|
automountConfig =
|
|
|
|
|
{ Where = config.where;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-09 00:59:59 +00:00
|
|
|
|
commonUnitText = def: lines: ''
|
2022-03-13 15:09:36 +00:00
|
|
|
|
[Unit]
|
|
|
|
|
${attrsToSection def.unitConfig}
|
2024-03-28 04:42:17 +00:00
|
|
|
|
'' + lines + optionalString (def.wantedBy != [ ]) ''
|
2023-11-09 00:59:59 +00:00
|
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
|
WantedBy=${concatStringsSep " " def.wantedBy}
|
2022-03-13 15:09:36 +00:00
|
|
|
|
'';
|
|
|
|
|
|
2024-03-21 13:52:12 +00:00
|
|
|
|
targetToUnit = def:
|
|
|
|
|
{ inherit (def) name aliases wantedBy requiredBy upheldBy enable overrideStrategy;
|
2022-03-13 15:09:36 +00:00
|
|
|
|
text =
|
|
|
|
|
''
|
|
|
|
|
[Unit]
|
|
|
|
|
${attrsToSection def.unitConfig}
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-21 13:52:12 +00:00
|
|
|
|
serviceToUnit = def:
|
|
|
|
|
{ inherit (def) name aliases wantedBy requiredBy upheldBy enable overrideStrategy;
|
2023-11-09 00:59:59 +00:00
|
|
|
|
text = commonUnitText def (''
|
2023-11-09 00:58:23 +00:00
|
|
|
|
[Service]
|
|
|
|
|
'' + (let env = cfg.globalEnvironment // def.environment;
|
|
|
|
|
in concatMapStrings (n:
|
|
|
|
|
let s = optionalString (env.${n} != null)
|
2024-03-28 04:42:17 +00:00
|
|
|
|
"Environment=${toJSON "${n}=${env.${n}}"}\n";
|
2023-11-09 00:58:23 +00:00
|
|
|
|
# systemd max line length is now 1MiB
|
|
|
|
|
# https://github.com/systemd/systemd/commit/e6dde451a51dc5aaa7f4d98d39b8fe735f73d2af
|
2024-03-21 13:52:12 +00:00
|
|
|
|
in if stringLength s >= 1048576 then throw "The value of the environment variable ‘${n}’ in systemd service ‘${def.name}.service’ is too long." else s) (attrNames env))
|
2023-11-09 00:58:23 +00:00
|
|
|
|
+ (if def ? reloadIfChanged && def.reloadIfChanged then ''
|
|
|
|
|
X-ReloadIfChanged=true
|
|
|
|
|
'' else if (def ? restartIfChanged && !def.restartIfChanged) then ''
|
|
|
|
|
X-RestartIfChanged=false
|
|
|
|
|
'' else "")
|
|
|
|
|
+ optionalString (def ? stopIfChanged && !def.stopIfChanged) ''
|
|
|
|
|
X-StopIfChanged=false
|
2023-11-09 00:59:59 +00:00
|
|
|
|
'' + attrsToSection def.serviceConfig);
|
2022-03-13 15:09:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
2024-03-21 13:52:12 +00:00
|
|
|
|
socketToUnit = def:
|
|
|
|
|
{ inherit (def) name aliases wantedBy requiredBy upheldBy enable overrideStrategy;
|
2023-11-09 00:59:59 +00:00
|
|
|
|
text = commonUnitText def ''
|
|
|
|
|
[Socket]
|
|
|
|
|
${attrsToSection def.socketConfig}
|
|
|
|
|
${concatStringsSep "\n" (map (s: "ListenStream=${s}") def.listenStreams)}
|
|
|
|
|
${concatStringsSep "\n" (map (s: "ListenDatagram=${s}") def.listenDatagrams)}
|
|
|
|
|
'';
|
2022-03-13 15:09:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
2024-03-21 13:52:12 +00:00
|
|
|
|
timerToUnit = def:
|
|
|
|
|
{ inherit (def) name aliases wantedBy requiredBy upheldBy enable overrideStrategy;
|
2023-11-09 00:59:59 +00:00
|
|
|
|
text = commonUnitText def ''
|
|
|
|
|
[Timer]
|
|
|
|
|
${attrsToSection def.timerConfig}
|
|
|
|
|
'';
|
2022-03-13 15:09:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
2024-03-21 13:52:12 +00:00
|
|
|
|
pathToUnit = def:
|
|
|
|
|
{ inherit (def) name aliases wantedBy requiredBy upheldBy enable overrideStrategy;
|
2023-11-09 00:59:59 +00:00
|
|
|
|
text = commonUnitText def ''
|
|
|
|
|
[Path]
|
|
|
|
|
${attrsToSection def.pathConfig}
|
|
|
|
|
'';
|
2022-03-13 15:09:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
2024-03-21 13:52:12 +00:00
|
|
|
|
mountToUnit = def:
|
|
|
|
|
{ inherit (def) name aliases wantedBy requiredBy upheldBy enable overrideStrategy;
|
2023-11-09 00:59:59 +00:00
|
|
|
|
text = commonUnitText def ''
|
|
|
|
|
[Mount]
|
|
|
|
|
${attrsToSection def.mountConfig}
|
|
|
|
|
'';
|
2022-03-13 15:09:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
2024-03-21 13:52:12 +00:00
|
|
|
|
automountToUnit = def:
|
|
|
|
|
{ inherit (def) name aliases wantedBy requiredBy upheldBy enable overrideStrategy;
|
2024-01-18 05:33:46 +00:00
|
|
|
|
text = commonUnitText def ''
|
|
|
|
|
[Automount]
|
|
|
|
|
${attrsToSection def.automountConfig}
|
|
|
|
|
'';
|
2022-03-13 15:09:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
2024-03-21 13:52:12 +00:00
|
|
|
|
sliceToUnit = def:
|
|
|
|
|
{ inherit (def) name aliases wantedBy requiredBy upheldBy enable overrideStrategy;
|
2023-11-09 00:59:59 +00:00
|
|
|
|
text = commonUnitText def ''
|
|
|
|
|
[Slice]
|
|
|
|
|
${attrsToSection def.sliceConfig}
|
|
|
|
|
'';
|
2022-03-13 15:09:36 +00:00
|
|
|
|
};
|
2023-07-26 21:30:08 +00:00
|
|
|
|
|
|
|
|
|
# Create a directory that contains systemd definition files from an attrset
|
|
|
|
|
# that contains the file names as keys and the content as values. The values
|
|
|
|
|
# in that attrset are determined by the supplied format.
|
|
|
|
|
definitions = directoryName: format: definitionAttrs:
|
|
|
|
|
let
|
2024-03-28 04:42:17 +00:00
|
|
|
|
listOfDefinitions = mapAttrsToList
|
2023-07-26 21:30:08 +00:00
|
|
|
|
(name: format.generate "${name}.conf")
|
|
|
|
|
definitionAttrs;
|
|
|
|
|
in
|
|
|
|
|
pkgs.runCommand directoryName { } ''
|
|
|
|
|
mkdir -p $out
|
2024-03-28 04:42:17 +00:00
|
|
|
|
${(concatStringsSep "\n"
|
2023-07-26 21:30:08 +00:00
|
|
|
|
(map (pkg: "cp ${pkg} $out/${pkg.name}") listOfDefinitions)
|
|
|
|
|
)}
|
|
|
|
|
'';
|
|
|
|
|
|
2024-04-08 14:36:28 +00:00
|
|
|
|
# The maximum number of characters allowed in a GPT partition label. This
|
|
|
|
|
# limit is specified by UEFI and enforced by systemd-repart.
|
|
|
|
|
# Corresponds to GPT_LABEL_MAX from systemd's gpt.h.
|
|
|
|
|
GPTMaxLabelLength = 36;
|
|
|
|
|
|
2015-04-20 09:31:17 +00:00
|
|
|
|
}
|