2016-11-05 21:47:09 +00:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
2017-01-02 07:21:27 +00:00
|
|
|
|
text = import ../lib/write-text.nix {
|
2016-11-05 21:47:09 +00:00
|
|
|
|
inherit lib;
|
|
|
|
|
mkTextDerivation = name: text: pkgs.writeText "etc-${name}" text;
|
|
|
|
|
};
|
|
|
|
|
|
2016-12-15 13:08:48 +00:00
|
|
|
|
hasDir = path: length (splitString "/" path) > 1;
|
|
|
|
|
|
2016-11-05 21:47:09 +00:00
|
|
|
|
etc = filter (f: f.enable) (attrValues config.environment.etc);
|
2023-05-09 10:27:49 +00:00
|
|
|
|
etcCopy = filter (f: f.copy) (attrValues config.environment.etc);
|
2016-12-15 13:08:48 +00:00
|
|
|
|
etcDirs = filter (attr: hasDir attr.target) (attrValues config.environment.etc);
|
2016-11-05 21:47:09 +00:00
|
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
|
|
environment.etc = mkOption {
|
2020-09-02 04:20:00 +00:00
|
|
|
|
type = types.attrsOf (types.submodule text);
|
2022-09-25 18:12:08 +00:00
|
|
|
|
default = { };
|
2023-06-22 11:21:32 +00:00
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
Set of files that have to be linked in {file}`/etc`.
|
2016-11-05 21:47:09 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
|
2022-09-25 18:12:08 +00:00
|
|
|
|
system.build.etc = pkgs.runCommand "etc"
|
2018-01-21 12:46:30 +00:00
|
|
|
|
{ preferLocalBuild = true; }
|
|
|
|
|
''
|
|
|
|
|
mkdir -p $out/etc
|
|
|
|
|
cd $out/etc
|
|
|
|
|
${concatMapStringsSep "\n" (attr: "mkdir -p $(dirname '${attr.target}')") etc}
|
|
|
|
|
${concatMapStringsSep "\n" (attr: "ln -s '${attr.source}' '${attr.target}'") etc}
|
2023-05-09 10:27:49 +00:00
|
|
|
|
${concatMapStringsSep "\n" (attr: "touch '${attr.target}'.copy") etcCopy}
|
2018-01-21 12:46:30 +00:00
|
|
|
|
'';
|
2016-11-05 21:47:09 +00:00
|
|
|
|
|
|
|
|
|
system.activationScripts.etc.text = ''
|
|
|
|
|
# Set up the statically computed bits of /etc.
|
2017-01-08 21:56:16 +00:00
|
|
|
|
echo "setting up /etc..." >&2
|
2016-11-05 21:47:09 +00:00
|
|
|
|
|
2020-06-17 16:29:20 +00:00
|
|
|
|
declare -A etcSha256Hashes
|
|
|
|
|
${concatMapStringsSep "\n" (attr: "etcSha256Hashes['/etc/${attr.target}']='${concatStringsSep " " attr.knownSha256Hashes}'") etc}
|
|
|
|
|
|
2016-11-05 21:47:09 +00:00
|
|
|
|
ln -sfn "$(readlink -f $systemConfig/etc)" /etc/static
|
|
|
|
|
|
2017-01-02 19:09:54 +00:00
|
|
|
|
for f in $(find /etc/static/* -type l); do
|
2018-01-08 18:08:02 +00:00
|
|
|
|
l=/etc/''${f#/etc/static/}
|
|
|
|
|
d=''${l%/*}
|
2017-01-02 19:09:54 +00:00
|
|
|
|
if [ ! -e "$d" ]; then
|
|
|
|
|
mkdir -p "$d"
|
|
|
|
|
fi
|
2023-05-10 14:33:21 +00:00
|
|
|
|
if [ -e "$f".copy ]; then
|
|
|
|
|
cp "$f" "$l"
|
|
|
|
|
continue
|
|
|
|
|
fi
|
2017-01-02 19:09:54 +00:00
|
|
|
|
if [ -e "$l" ]; then
|
2020-06-17 16:29:20 +00:00
|
|
|
|
if [ "$(readlink "$l")" != "$f" ]; then
|
2018-01-13 17:10:48 +00:00
|
|
|
|
if ! grep -q /etc/static "$l"; then
|
2020-06-17 16:29:20 +00:00
|
|
|
|
o=''$(shasum -a256 "$l")
|
|
|
|
|
o=''${o%% *}
|
|
|
|
|
for h in ''${etcSha256Hashes["$l"]}; do
|
|
|
|
|
if [ "$o" = "$h" ]; then
|
|
|
|
|
mv "$l" "$l.orig"
|
2023-05-10 14:33:21 +00:00
|
|
|
|
ln -s "$f" "$l"
|
2020-06-17 16:29:20 +00:00
|
|
|
|
break
|
|
|
|
|
else
|
|
|
|
|
h=
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
if [ -z "$h" ]; then
|
|
|
|
|
echo "[1;31merror: not linking environment.etc.\"''${l#/etc/}\" because $l already exists, skipping...[0m" >&2
|
|
|
|
|
echo "[1;31mexisting file has unknown content $o, move and activate again to apply[0m" >&2
|
|
|
|
|
fi
|
2018-01-13 17:10:48 +00:00
|
|
|
|
fi
|
2016-11-05 21:47:09 +00:00
|
|
|
|
fi
|
|
|
|
|
else
|
2023-05-10 14:33:21 +00:00
|
|
|
|
ln -s "$f" "$l"
|
2016-11-05 21:47:09 +00:00
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
2017-01-02 19:09:54 +00:00
|
|
|
|
for l in $(find /etc/* -type l 2> /dev/null); do
|
|
|
|
|
f="$(echo $l | sed 's,/etc/,/etc/static/,')"
|
2018-01-08 18:08:02 +00:00
|
|
|
|
f=/etc/static/''${l#/etc/}
|
2020-06-17 16:29:20 +00:00
|
|
|
|
if [ "$(readlink "$l")" = "$f" -a ! -e "$(readlink -f "$l")" ]; then
|
2017-01-02 19:09:54 +00:00
|
|
|
|
rm "$l"
|
2016-11-05 21:47:09 +00:00
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|