-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmanaged-files.mod.nix
More file actions
191 lines (171 loc) · 5.07 KB
/
Copy pathmanaged-files.mod.nix
File metadata and controls
191 lines (171 loc) · 5.07 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
{
flake.modularServices.managed-files =
{
config,
lib,
name,
options,
...
}:
let
inherit (lib) baseNameOf dirOf toFile;
inherit (lib.attrsets) filterAttrs mapAttrsToList optionalAttrs;
inherit (lib.lists) singleton;
inherit (lib.meta) getExe;
inherit (lib.modules) mkDerivedConfig mkIf;
inherit (lib.options) mkOption;
inherit (lib.strings) escapeShellArgs toJSON;
inherit (lib.types)
attrsOf
enum
ints
lines
nullOr
package
path
str
submodule
;
cfg = config.managed-files;
manifestCurrent = config.configData."manifest.json".path;
manifestNew = toFile "managed-files-${name}.json" (
{
version = 3;
clobber_by_default = true;
files =
cfg.files
|> mapAttrsToList (
target: file:
filterAttrs (_: value: value != null) {
inherit (file)
type
source
permissions
uid
gid
;
inherit target;
}
);
}
|> toJSON
);
smfh = getExe cfg.smfh;
in
{
_class = "service";
options.managed-files = {
smfh = mkOption {
type = package;
description = "smfh package used to apply the manifest.";
};
nushell = mkOption {
type = package;
description = "Nushell package used to build the activation script.";
};
files = mkOption {
default = { };
description = "Managed files.";
type =
attrsOf
<| submodule (
{
name,
config,
options,
...
}:
{
options = {
type = mkOption {
type = enum [
"copy"
"symlink"
"directory"
"delete"
"modify"
];
default = "copy";
description = "Type of path to create.";
};
text = mkOption {
type = nullOr lines;
default = null;
description = "Text of the file.";
};
source = mkOption {
type = nullOr path;
default = null;
description = "Path of the source file or directory.";
};
permissions = mkOption {
type = nullOr str;
default = null;
description = "Permissions (in octal) to set on the target path.";
};
uid = mkOption {
type = nullOr ints.u32;
default = null;
description = "User ID to set as owner on the target path.";
};
gid = mkOption {
type = nullOr ints.u32;
default = null;
description = "Group ID to set as owner on the target path.";
};
};
config.source = mkIf (config.text != null) (
mkDerivedConfig options.text (toFile (baseNameOf name))
);
}
);
};
};
config = {
# We write the "last applied manifest" ourselves, so it cannot be generated declaratively.
configData."manifest.json".enable = false;
process.argv =
singleton
<| getExe
<| cfg.nushell.stdenv.mkDerivation {
name = "managed-files-${name}";
meta.mainProgram = "managed-files";
passAsFile = singleton "text";
text = /* nu */ ''
#!${getExe cfg.nushell}
#
mkdir ${dirOf manifestCurrent}
^${smfh} diff ${manifestNew} ${manifestCurrent} --fallback
^${smfh} activate ${manifestNew}
cp --force ${manifestNew} ${manifestCurrent}
'';
phases = singleton "installPhase";
installPhase = ''
install -D --mode 755 "$textPath" "$out/bin/managed-files"
'';
};
}
// optionalAttrs (options ? launchd) {
launchd = {
RunAtLoad = true;
ProgramArguments = [
"/bin/sh"
"-c"
/* bash */ ''
/bin/wait4path /nix/store && exec ${escapeShellArgs config.process.argv}
''
];
};
}
// optionalAttrs (options ? systemd) {
systemd.service = {
wantedBy = [
"multi-user.target"
"sysinit-reactivation.target"
];
after = singleton "local-fs.target";
serviceConfig.Type = "oneshot";
};
};
};
}