2020-04-24 23:36:52 +00:00
|
|
|
# Management of static files in /etc.
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
2021-10-04 12:37:57 +00:00
|
|
|
etc' = filter (f: f.enable) (attrValues config.environment.etc);
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-08-08 23:34:03 +00:00
|
|
|
etc = pkgs.runCommandLocal "etc" {
|
2021-08-05 21:33:18 +00:00
|
|
|
# This is needed for the systemd module
|
|
|
|
passthru.targets = map (x: x.target) etc';
|
|
|
|
} /* sh */ ''
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
makeEtcEntry() {
|
|
|
|
src="$1"
|
|
|
|
target="$2"
|
|
|
|
mode="$3"
|
|
|
|
user="$4"
|
|
|
|
group="$5"
|
|
|
|
|
|
|
|
if [[ "$src" = *'*'* ]]; then
|
|
|
|
# If the source name contains '*', perform globbing.
|
|
|
|
mkdir -p "$out/etc/$target"
|
|
|
|
for fn in $src; do
|
|
|
|
ln -s "$fn" "$out/etc/$target/"
|
|
|
|
done
|
|
|
|
else
|
|
|
|
|
|
|
|
mkdir -p "$out/etc/$(dirname "$target")"
|
|
|
|
if ! [ -e "$out/etc/$target" ]; then
|
|
|
|
ln -s "$src" "$out/etc/$target"
|
|
|
|
else
|
|
|
|
echo "duplicate entry $target -> $src"
|
|
|
|
if [ "$(readlink "$out/etc/$target")" != "$src" ]; then
|
|
|
|
echo "mismatched duplicate entry $(readlink "$out/etc/$target") <-> $src"
|
|
|
|
ret=1
|
|
|
|
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$mode" != symlink ]; then
|
|
|
|
echo "$mode" > "$out/etc/$target.mode"
|
|
|
|
echo "$user" > "$out/etc/$target.uid"
|
|
|
|
echo "$group" > "$out/etc/$target.gid"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir -p "$out/etc"
|
|
|
|
${concatMapStringsSep "\n" (etcEntry: escapeShellArgs [
|
|
|
|
"makeEtcEntry"
|
2021-10-04 12:37:57 +00:00
|
|
|
# Force local source paths to be added to the store
|
|
|
|
"${etcEntry.source}"
|
2021-08-05 21:33:18 +00:00
|
|
|
etcEntry.target
|
|
|
|
etcEntry.mode
|
|
|
|
etcEntry.user
|
|
|
|
etcEntry.group
|
|
|
|
]) etc'}
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
2022-02-10 20:34:41 +00:00
|
|
|
imports = [ ../build.nix ];
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
environment.etc = mkOption {
|
|
|
|
default = {};
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression ''
|
2020-04-24 23:36:52 +00:00
|
|
|
{ example-configuration-file =
|
|
|
|
{ source = "/nix/store/.../etc/dir/file.conf.example";
|
|
|
|
mode = "0440";
|
|
|
|
};
|
|
|
|
"default/useradd".text = "GROUP=100 ...";
|
|
|
|
}
|
|
|
|
'';
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Set of files that have to be linked in {file}`/etc`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
type = with types; attrsOf (submodule (
|
2021-12-06 16:07:01 +00:00
|
|
|
{ name, config, options, ... }:
|
2020-04-24 23:36:52 +00:00
|
|
|
{ options = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Whether this /etc file should be generated. This
|
|
|
|
option allows specific /etc files to be disabled.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
target = mkOption {
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Name of symlink (relative to
|
2022-08-12 12:06:08 +00:00
|
|
|
{file}`/etc`). Defaults to the attribute
|
2020-04-24 23:36:52 +00:00
|
|
|
name.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
text = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.lines;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Text of the file.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
source = mkOption {
|
|
|
|
type = types.path;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Path of the source file.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mode = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "symlink";
|
|
|
|
example = "0600";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
If set to something else than `symlink`,
|
2020-04-24 23:36:52 +00:00
|
|
|
the file is copied instead of symlinked, with the given
|
|
|
|
file mode.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
uid = mkOption {
|
|
|
|
default = 0;
|
|
|
|
type = types.int;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
UID of created file. Only takes effect when the file is
|
|
|
|
copied (that is, the mode is not 'symlink').
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
gid = mkOption {
|
|
|
|
default = 0;
|
|
|
|
type = types.int;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
GID of created file. Only takes effect when the file is
|
|
|
|
copied (that is, the mode is not 'symlink').
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
default = "+${toString config.uid}";
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
User name of created file.
|
|
|
|
Only takes effect when the file is copied (that is, the mode is not 'symlink').
|
2022-08-12 12:06:08 +00:00
|
|
|
Changing this option takes precedence over `uid`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
default = "+${toString config.gid}";
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Group name of created file.
|
|
|
|
Only takes effect when the file is copied (that is, the mode is not 'symlink').
|
2022-08-12 12:06:08 +00:00
|
|
|
Changing this option takes precedence over `gid`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
target = mkDefault name;
|
|
|
|
source = mkIf (config.text != null) (
|
|
|
|
let name' = "etc-" + baseNameOf name;
|
2021-12-06 16:07:01 +00:00
|
|
|
in mkDerivedConfig options.text (pkgs.writeText name')
|
|
|
|
);
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
|
|
|
system.build.etc = etc;
|
2022-02-10 20:34:41 +00:00
|
|
|
system.build.etcActivationCommands =
|
2020-04-24 23:36:52 +00:00
|
|
|
''
|
|
|
|
# Set up the statically computed bits of /etc.
|
|
|
|
echo "setting up /etc..."
|
2021-04-12 18:23:04 +00:00
|
|
|
${pkgs.perl.withPackages (p: [ p.FileSlurp ])}/bin/perl ${./setup-etc.pl} ${etc}/etc
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|