2024-12-10 20:26:33 +01:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
utils,
|
|
|
|
...
|
|
|
|
}:
|
2016-10-08 16:10:56 +02:00
|
|
|
|
2021-11-20 12:34:13 -05:00
|
|
|
with utils.systemdUtils.unitOptions;
|
|
|
|
with utils.systemdUtils.lib;
|
2016-10-08 16:10:56 +02:00
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.systemd.nspawn;
|
|
|
|
|
|
|
|
checkExec = checkUnitConfig "Exec" [
|
|
|
|
(assertOnlyFields [
|
2024-12-10 20:26:33 +01:00
|
|
|
"Boot"
|
|
|
|
"ProcessTwo"
|
|
|
|
"Parameters"
|
|
|
|
"Environment"
|
|
|
|
"User"
|
|
|
|
"WorkingDirectory"
|
|
|
|
"PivotRoot"
|
|
|
|
"Capability"
|
|
|
|
"DropCapability"
|
|
|
|
"NoNewPrivileges"
|
|
|
|
"KillSignal"
|
|
|
|
"Personality"
|
|
|
|
"MachineID"
|
|
|
|
"PrivateUsers"
|
|
|
|
"NotifyReady"
|
|
|
|
"SystemCallFilter"
|
|
|
|
"LimitCPU"
|
|
|
|
"LimitFSIZE"
|
|
|
|
"LimitDATA"
|
|
|
|
"LimitSTACK"
|
|
|
|
"LimitCORE"
|
|
|
|
"LimitRSS"
|
|
|
|
"LimitNOFILE"
|
|
|
|
"LimitAS"
|
|
|
|
"LimitNPROC"
|
|
|
|
"LimitMEMLOCK"
|
|
|
|
"LimitLOCKS"
|
|
|
|
"LimitSIGPENDING"
|
|
|
|
"LimitMSGQUEUE"
|
|
|
|
"LimitNICE"
|
|
|
|
"LimitRTPRIO"
|
|
|
|
"LimitRTTIME"
|
|
|
|
"OOMScoreAdjust"
|
|
|
|
"CPUAffinity"
|
|
|
|
"Hostname"
|
|
|
|
"ResolvConf"
|
|
|
|
"Timezone"
|
|
|
|
"LinkJournal"
|
|
|
|
"Ephemeral"
|
|
|
|
"AmbientCapability"
|
2016-10-08 16:10:56 +02:00
|
|
|
])
|
|
|
|
(assertValueOneOf "Boot" boolValues)
|
|
|
|
(assertValueOneOf "ProcessTwo" boolValues)
|
2017-05-20 20:32:45 +01:00
|
|
|
(assertValueOneOf "NotifyReady" boolValues)
|
2016-10-08 16:10:56 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
checkFiles = checkUnitConfig "Files" [
|
|
|
|
(assertOnlyFields [
|
2024-12-10 20:26:33 +01:00
|
|
|
"ReadOnly"
|
|
|
|
"Volatile"
|
|
|
|
"Bind"
|
|
|
|
"BindReadOnly"
|
|
|
|
"TemporaryFileSystem"
|
|
|
|
"Overlay"
|
|
|
|
"OverlayReadOnly"
|
|
|
|
"PrivateUsersChown"
|
|
|
|
"BindUser"
|
|
|
|
"Inaccessible"
|
|
|
|
"PrivateUsersOwnership"
|
2016-10-08 16:10:56 +02:00
|
|
|
])
|
|
|
|
(assertValueOneOf "ReadOnly" boolValues)
|
|
|
|
(assertValueOneOf "Volatile" (boolValues ++ [ "state" ]))
|
|
|
|
(assertValueOneOf "PrivateUsersChown" boolValues)
|
2024-12-10 20:26:33 +01:00
|
|
|
(assertValueOneOf "PrivateUsersOwnership" [
|
|
|
|
"off"
|
|
|
|
"chown"
|
|
|
|
"map"
|
|
|
|
"auto"
|
|
|
|
])
|
2016-10-08 16:10:56 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
checkNetwork = checkUnitConfig "Network" [
|
|
|
|
(assertOnlyFields [
|
2024-12-10 20:26:33 +01:00
|
|
|
"Private"
|
|
|
|
"VirtualEthernet"
|
|
|
|
"VirtualEthernetExtra"
|
|
|
|
"Interface"
|
|
|
|
"MACVLAN"
|
|
|
|
"IPVLAN"
|
|
|
|
"Bridge"
|
|
|
|
"Zone"
|
|
|
|
"Port"
|
2016-10-08 16:10:56 +02:00
|
|
|
])
|
|
|
|
(assertValueOneOf "Private" boolValues)
|
|
|
|
(assertValueOneOf "VirtualEthernet" boolValues)
|
|
|
|
];
|
|
|
|
|
|
|
|
instanceOptions = {
|
2024-12-10 20:26:33 +01:00
|
|
|
options = (getAttrs [ "enable" ] sharedOptions) // {
|
2016-10-21 01:31:54 +09:00
|
|
|
execConfig = mkOption {
|
2024-12-10 20:26:33 +01:00
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
Parameters = "/bin/sh";
|
|
|
|
};
|
2016-10-21 01:31:54 +09:00
|
|
|
type = types.addCheck (types.attrsOf unitOption) checkExec;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2016-10-21 01:31:54 +09:00
|
|
|
Each attribute in this set specifies an option in the
|
2022-08-05 19:39:00 +02:00
|
|
|
`[Exec]` section of this unit. See
|
|
|
|
{manpage}`systemd.nspawn(5)` for details.
|
2016-10-21 01:31:54 +09:00
|
|
|
'';
|
|
|
|
};
|
2016-10-08 16:10:56 +02:00
|
|
|
|
2016-10-21 01:31:54 +09:00
|
|
|
filesConfig = mkOption {
|
2024-12-10 20:26:33 +01:00
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
Bind = [ "/home/alice" ];
|
|
|
|
};
|
2016-10-21 01:31:54 +09:00
|
|
|
type = types.addCheck (types.attrsOf unitOption) checkFiles;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2016-10-21 01:31:54 +09:00
|
|
|
Each attribute in this set specifies an option in the
|
2022-08-05 19:39:00 +02:00
|
|
|
`[Files]` section of this unit. See
|
|
|
|
{manpage}`systemd.nspawn(5)` for details.
|
2016-10-21 01:31:54 +09:00
|
|
|
'';
|
|
|
|
};
|
2016-10-08 16:10:56 +02:00
|
|
|
|
2016-10-21 01:31:54 +09:00
|
|
|
networkConfig = mkOption {
|
2024-12-10 20:26:33 +01:00
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
Private = false;
|
|
|
|
};
|
2016-10-21 01:31:54 +09:00
|
|
|
type = types.addCheck (types.attrsOf unitOption) checkNetwork;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2016-10-21 01:31:54 +09:00
|
|
|
Each attribute in this set specifies an option in the
|
2022-08-05 19:39:00 +02:00
|
|
|
`[Network]` section of this unit. See
|
|
|
|
{manpage}`systemd.nspawn(5)` for details.
|
2016-10-21 01:31:54 +09:00
|
|
|
'';
|
|
|
|
};
|
2016-10-08 16:10:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2024-12-10 20:26:33 +01:00
|
|
|
instanceToUnit =
|
|
|
|
name: def:
|
|
|
|
let
|
|
|
|
base = {
|
|
|
|
text = ''
|
|
|
|
[Exec]
|
|
|
|
${attrsToSection def.execConfig}
|
2016-10-08 16:10:56 +02:00
|
|
|
|
2024-12-10 20:26:33 +01:00
|
|
|
[Files]
|
|
|
|
${attrsToSection def.filesConfig}
|
2016-10-08 16:10:56 +02:00
|
|
|
|
2024-12-10 20:26:33 +01:00
|
|
|
[Network]
|
|
|
|
${attrsToSection def.networkConfig}
|
|
|
|
'';
|
|
|
|
} // def;
|
|
|
|
in
|
|
|
|
base // { unit = makeUnit name base; };
|
2016-10-08 16:10:56 +02:00
|
|
|
|
2024-12-10 20:26:33 +01:00
|
|
|
in
|
|
|
|
{
|
2016-10-08 16:10:56 +02:00
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
systemd.nspawn = mkOption {
|
2024-12-10 20:26:33 +01:00
|
|
|
default = { };
|
2016-10-21 01:31:54 +09:00
|
|
|
type = with types; attrsOf (submodule instanceOptions);
|
2024-04-13 14:54:15 +02:00
|
|
|
description = "Definition of systemd-nspawn configurations.";
|
2016-10-08 16:10:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config =
|
|
|
|
let
|
2024-12-10 20:26:33 +01:00
|
|
|
units = mapAttrs' (
|
|
|
|
n: v:
|
|
|
|
let
|
|
|
|
nspawnFile = "${n}.nspawn";
|
|
|
|
in
|
|
|
|
nameValuePair nspawnFile (instanceToUnit nspawnFile v)
|
|
|
|
) cfg;
|
2020-08-07 14:43:58 +01:00
|
|
|
in
|
2024-12-10 20:26:33 +01:00
|
|
|
mkMerge [
|
|
|
|
(mkIf (cfg != { }) {
|
|
|
|
environment.etc."systemd/nspawn".source = mkIf (cfg != { }) (generateUnits {
|
|
|
|
allowCollisions = false;
|
|
|
|
type = "nspawn";
|
|
|
|
inherit units;
|
|
|
|
upstreamUnits = [ ];
|
|
|
|
upstreamWants = [ ];
|
|
|
|
});
|
|
|
|
})
|
|
|
|
{
|
|
|
|
systemd.targets.multi-user.wants = [ "machines.target" ];
|
|
|
|
systemd.services."systemd-nspawn@".environment = {
|
|
|
|
SYSTEMD_NSPAWN_UNIFIED_HIERARCHY = mkDefault "1";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
2016-10-08 16:10:56 +02:00
|
|
|
}
|