-
Notifications
You must be signed in to change notification settings - Fork 12
/
home-manager-module.nix
134 lines (129 loc) · 4.68 KB
/
home-manager-module.nix
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
{ libUtils }:
{
config,
osConfig ? { },
lib,
pkgs,
...
}:
let
inherit (lib) types mkOption attrValues mergeAttrsList mkIf getExe;
cfg = config.virtualisation.quadlet;
quadletUtils = import ./utils.nix {
inherit lib;
systemdUtils = (libUtils { inherit lib config pkgs; }).systemdUtils;
isUserSystemd = true;
podmanPackage = osConfig.virtualisation.podman.package or pkgs.podman;
};
containerOpts = types.submodule (import ./container.nix { inherit quadletUtils; });
networkOpts = types.submodule (import ./network.nix { inherit quadletUtils; });
podOpts = types.submodule (import ./pod.nix { inherit quadletUtils; });
activationScript = lib.hm.dag.entryBefore [ "reloadSystemd" ] ''
mkdir -p '${config.xdg.configHome}/quadlet-nix/'
ln -sf "''${XDG_RUNTIME_DIR:-/run/user/$UID}/systemd/generator/" '${config.xdg.configHome}/quadlet-nix/out'
'';
in
{
options.virtualisation.quadlet = {
autoUpdate = {
enable = mkOption {
type = types.bool;
default = false;
};
calendar = mkOption {
type = types.str;
default = "*-*-* 00:00:00";
};
};
containers = mkOption {
type = types.attrsOf containerOpts;
default = { };
};
networks = mkOption {
type = types.attrsOf networkOpts;
default = { };
};
pods = mkOption {
type = types.attrsOf podOpts;
default = { };
};
};
config =
let
allObjects = (attrValues cfg.containers) ++ (attrValues cfg.networks) ++ (attrValues cfg.pods);
in
{
home.activation.quadletNix = mkIf (lib.length allObjects > 0) activationScript;
xdg.configFile =
let
configPathLink = (pkgs.linkFarm "quadlet-out-path" [{
name = "quadlet-nix";
path = "${config.xdg.configHome}/quadlet-nix";
}]) + "/quadlet-nix";
in
mergeAttrsList (
map (p: {
# Install the .container, .network, etc files
"containers/systemd/${p.ref}" = {
text = p._configText;
};
# Import quadlet-generated unit as a dropin override.
"systemd/user/${p._serviceName}.service.d/override.conf" = {
source = "${configPathLink}/out/${p._serviceName}.service";
};
}) allObjects
) // {
# the stock service uses `sh` instead of `/bin/sh`.
# systemd only looks for command binary in a few static location.
# See: https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html#Command%20lines
"systemd/user/podman-user-wait-network-online.service.d/override.conf" = {
text = quadletUtils.unitConfigToText {
Service.ExecSearchPath = "/bin";
Install.WantedBy = [ "default.target" ];
};
};
};
systemd.user.services = mergeAttrsList (
map (p: {
# Inject hash for the activation process to detect changes.
# Must be in the main file as it's the only thing home-manager switch process looks at.
# WantedBy must be set through `systemd.user.services` which generates .targets.wants symlinks.
# sd-switch only starts new services with those symlinks.
${p._serviceName} = {
Unit.X-QuadletNixConfigHash = builtins.hashString "sha256" p._configText;
Service.Environment = [ "PATH=/run/wrappers/bin" ];
Install.WantedBy = p._wantedBy;
};
}) allObjects
) // {
# TODO: link from ${pkgs.podman}/share/systemd/user/podman-auto-update.service
# when https://github.com/containers/podman/issues/24637 is fixed.
podman-auto-update = mkIf cfg.autoUpdate.enable {
Unit = {
Description = "Podman auto-update service";
Documentation = "man:podman-auto-update(1)";
};
Service = {
Type = "oneshot";
# podman rootless requires "newuidmap" (the suid version, not the non-suid one from pkgs.shadow)
Environment = "PATH=/run/wrappers/bin";
ExecStart = "${getExe quadletUtils.podmanPackage} auto-update";
ExecStartPost = "${getExe quadletUtils.podmanPackage} image prune -f";
TimeoutStartSec = "900s";
TimeoutStopSec = "10s";
};
};
};
systemd.user.timers.podman-auto-update = mkIf cfg.autoUpdate.enable {
Unit = {
Description = "Podman auto-update timer";
Documentation = "man:podman-auto-update(1)";
};
Timer = {
OnCalendar = cfg.autoUpdate.calendar;
Persistent = true;
};
Install.WantedBy = [ "timers.target" ];
};
};
}