2013-02-03 10:35:11 +01:00
|
|
|
# Management of static files in /etc.
|
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2010-09-13 17:41:38 +02:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2006-12-11 16:32:10 +01:00
|
|
|
|
2009-01-02 17:06:52 +01:00
|
|
|
let
|
|
|
|
|
2021-09-02 15:43:57 +02:00
|
|
|
# if the source is a local file, it should be imported to the store
|
|
|
|
localToStore = mapAttrs (name: value: if name == "source" then "${value}" else value);
|
|
|
|
etc' = map localToStore (filter (f: f.enable) (attrValues config.environment.etc));
|
2013-02-03 13:24:22 +01:00
|
|
|
|
2021-08-07 20:56:21 +02:00
|
|
|
etc = pkgs.runCommandLocal "etc" {
|
2021-07-22 17:38:31 +02: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"
|
|
|
|
etcEntry.source
|
|
|
|
etcEntry.target
|
|
|
|
etcEntry.mode
|
|
|
|
etcEntry.user
|
|
|
|
etcEntry.group
|
|
|
|
]) etc'}
|
|
|
|
'';
|
2013-02-03 10:35:11 +01:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
2009-05-28 15:17:56 +02:00
|
|
|
environment.etc = mkOption {
|
2013-02-03 13:24:22 +01:00
|
|
|
default = {};
|
2013-10-30 16:19:07 +01:00
|
|
|
example = literalExample ''
|
2016-04-27 17:27:52 +02:00
|
|
|
{ example-configuration-file =
|
2013-02-03 13:24:22 +01:00
|
|
|
{ source = "/nix/store/.../etc/dir/file.conf.example";
|
|
|
|
mode = "0440";
|
|
|
|
};
|
|
|
|
"default/useradd".text = "GROUP=100 ...";
|
2013-10-30 16:19:07 +01:00
|
|
|
}
|
|
|
|
'';
|
2009-05-28 15:17:56 +02:00
|
|
|
description = ''
|
2013-02-03 13:24:22 +01:00
|
|
|
Set of files that have to be linked in <filename>/etc</filename>.
|
2009-05-28 15:17:56 +02:00
|
|
|
'';
|
2013-02-03 13:24:22 +01:00
|
|
|
|
2020-08-23 01:28:45 +02:00
|
|
|
type = with types; attrsOf (submodule (
|
2016-09-11 12:35:42 +02:00
|
|
|
{ name, config, ... }:
|
2013-02-03 13:24:22 +01:00
|
|
|
{ options = {
|
|
|
|
|
2013-02-03 14:19:05 +01:00
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether this /etc file should be generated. This
|
|
|
|
option allows specific /etc files to be disabled.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-02-03 13:24:22 +01:00
|
|
|
target = mkOption {
|
2013-10-30 17:37:45 +01:00
|
|
|
type = types.str;
|
2013-02-03 13:24:22 +01:00
|
|
|
description = ''
|
|
|
|
Name of symlink (relative to
|
|
|
|
<filename>/etc</filename>). Defaults to the attribute
|
|
|
|
name.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-02-03 14:12:49 +01:00
|
|
|
text = mkOption {
|
|
|
|
default = null;
|
2013-10-30 17:37:45 +01:00
|
|
|
type = types.nullOr types.lines;
|
2013-02-03 14:12:49 +01:00
|
|
|
description = "Text of the file.";
|
|
|
|
};
|
|
|
|
|
|
|
|
source = mkOption {
|
2013-10-28 15:12:11 +01:00
|
|
|
type = types.path;
|
2013-02-03 14:12:49 +01:00
|
|
|
description = "Path of the source file.";
|
|
|
|
};
|
|
|
|
|
2013-02-03 13:24:22 +01:00
|
|
|
mode = mkOption {
|
2013-10-30 17:37:45 +01:00
|
|
|
type = types.str;
|
2013-02-03 13:24:22 +01:00
|
|
|
default = "symlink";
|
|
|
|
example = "0600";
|
|
|
|
description = ''
|
|
|
|
If set to something else than <literal>symlink</literal>,
|
|
|
|
the file is copied instead of symlinked, with the given
|
|
|
|
file mode.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-02-18 09:09:01 +01:00
|
|
|
uid = mkOption {
|
|
|
|
default = 0;
|
|
|
|
type = types.int;
|
|
|
|
description = ''
|
2020-03-09 12:01:19 +01:00
|
|
|
UID of created file. Only takes effect when the file is
|
2014-02-18 09:09:01 +01:00
|
|
|
copied (that is, the mode is not 'symlink').
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
gid = mkOption {
|
|
|
|
default = 0;
|
|
|
|
type = types.int;
|
|
|
|
description = ''
|
2020-03-09 12:01:19 +01:00
|
|
|
GID of created file. Only takes effect when the file is
|
2014-02-18 09:09:01 +01:00
|
|
|
copied (that is, the mode is not 'symlink').
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-07-21 16:41:19 +02:00
|
|
|
user = mkOption {
|
|
|
|
default = "+${toString config.uid}";
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
User name of created file.
|
2020-03-09 12:01:19 +01:00
|
|
|
Only takes effect when the file is copied (that is, the mode is not 'symlink').
|
2017-07-21 16:41:19 +02:00
|
|
|
Changing this option takes precedence over <literal>uid</literal>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
default = "+${toString config.gid}";
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
Group name of created file.
|
2020-03-09 12:01:19 +01:00
|
|
|
Only takes effect when the file is copied (that is, the mode is not 'symlink').
|
2017-07-21 16:41:19 +02:00
|
|
|
Changing this option takes precedence over <literal>gid</literal>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-02-03 13:24:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
target = mkDefault name;
|
2015-12-30 15:13:43 +01:00
|
|
|
source = mkIf (config.text != null) (
|
|
|
|
let name' = "etc-" + baseNameOf name;
|
|
|
|
in mkDefault (pkgs.writeText name' config.text));
|
2013-02-03 13:24:22 +01:00
|
|
|
};
|
|
|
|
|
2016-09-11 12:35:42 +02:00
|
|
|
}));
|
2013-02-03 13:24:22 +01:00
|
|
|
|
2009-01-02 17:06:52 +01:00
|
|
|
};
|
2013-02-03 10:35:11 +01:00
|
|
|
|
2009-01-02 17:06:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-02-03 10:35:11 +01:00
|
|
|
###### implementation
|
2009-08-16 15:14:33 +02:00
|
|
|
|
2013-02-03 10:35:11 +01:00
|
|
|
config = {
|
2009-08-16 15:14:33 +02:00
|
|
|
|
2013-02-03 10:35:11 +01:00
|
|
|
system.build.etc = etc;
|
2012-05-09 23:35:47 +02:00
|
|
|
|
2017-07-21 16:41:19 +02:00
|
|
|
system.activationScripts.etc = stringAfter [ "users" "groups" ]
|
2013-02-03 10:35:11 +01:00
|
|
|
''
|
|
|
|
# Set up the statically computed bits of /etc.
|
|
|
|
echo "setting up /etc..."
|
2021-02-24 20:53:45 +01:00
|
|
|
${pkgs.perl.withPackages (p: [ p.FileSlurp ])}/bin/perl ${./setup-etc.pl} ${etc}/etc
|
2013-02-03 10:35:11 +01:00
|
|
|
'';
|
2009-01-02 17:06:52 +01:00
|
|
|
|
2013-02-03 10:35:11 +01:00
|
|
|
};
|
2009-10-30 09:37:08 +01:00
|
|
|
|
2007-02-26 22:18:13 +01:00
|
|
|
}
|