1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2024-12-15 17:51:01 +00:00
nix-darwin/modules/system/etc.nix

95 lines
2.6 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
with lib;
let
2017-01-02 07:21:27 +00:00
text = import ../lib/write-text.nix {
inherit lib;
mkTextDerivation = name: text: pkgs.writeText "etc-${name}" text;
};
2016-12-15 13:08:48 +00:00
hasDir = path: length (splitString "/" path) > 1;
etc = filter (f: f.enable) (attrValues config.environment.etc);
2016-12-15 13:08:48 +00:00
etcDirs = filter (attr: hasDir attr.target) (attrValues config.environment.etc);
in
{
options = {
environment.etc = mkOption {
type = types.attrsOf (types.submodule text);
default = {};
description = ''
Set of files that have to be linked in <filename>/etc</filename>.
'';
};
};
config = {
system.build.etc = pkgs.runCommandNoCC "etc"
{ 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}
'';
system.activationScripts.etc.text = ''
# Set up the statically computed bits of /etc.
echo "setting up /etc..." >&2
declare -A etcSha256Hashes
${concatMapStringsSep "\n" (attr: "etcSha256Hashes['/etc/${attr.target}']='${concatStringsSep " " attr.knownSha256Hashes}'") etc}
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
l=/etc/''${f#/etc/static/}
d=''${l%/*}
2017-01-02 19:09:54 +00:00
if [ ! -e "$d" ]; then
mkdir -p "$d"
fi
if [ -e "$l" ]; then
if [ "$(readlink "$l")" != "$f" ]; then
if ! grep -q /etc/static "$l"; then
o=''$(shasum -a256 "$l")
o=''${o%% *}
for h in ''${etcSha256Hashes["$l"]}; do
if [ "$o" = "$h" ]; then
mv "$l" "$l.orig"
ln -s "$f" "$l"
break
else
h=
fi
done
if [ -z "$h" ]; then
echo "error: not linking environment.etc.\"''${l#/etc/}\" because $l already exists, skipping..." >&2
echo "existing file has unknown content $o, move and activate again to apply" >&2
fi
fi
fi
else
2017-01-02 19:09:54 +00:00
ln -s "$f" "$l"
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/,')"
f=/etc/static/''${l#/etc/}
if [ "$(readlink "$l")" = "$f" -a ! -e "$(readlink -f "$l")" ]; then
2017-01-02 19:09:54 +00:00
rm "$l"
fi
done
'';
};
}