summaryrefslogtreecommitdiff
path: root/modules/system/etc.nix
blob: b99ca99225ca8a5a322ebb07da41a6f142c3f6ea (plain)
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
{ config, lib, pkgs, ... }:

with lib;

let

  text = import ../lib/write-text.nix {
    inherit lib;
    mkTextDerivation = name: text: pkgs.writeText "etc-${name}" text;
  };

  hasDir = path: length (splitString "/" path) > 1;

  etc = filter (f: f.enable) (attrValues config.environment.etc);
  etcDirs = filter (attr: hasDir attr.target) (attrValues config.environment.etc);

in

{
  options = {

    environment.etc = mkOption {
      type = types.loaOf (types.submodule text);
      default = {};
      description = ''
        Set of files that have to be linked in <filename>/etc</filename>.
      '';
    };

  };

  config = {

    system.build.etc = pkgs.runCommand "etc" {} ''
      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

      ln -sfn "$(readlink -f $systemConfig/etc)" /etc/static

      for f in $(find /etc/static/* -type l); do
        l="$(echo $f | sed 's,/etc/static/,/etc/,')"
        d="$(dirname $l)"
        if [ ! -e "$d" ]; then
          mkdir -p "$d"
        fi
        if [ -e "$l" ]; then
          if [ "$(readlink $l)" != "$f" ]; then
            echo "warning: not linking $l because $l exists, skipping..." >&2
          fi
        else
          ln -s "$f" "$l"
        fi
      done

      for l in $(find /etc/* -type l 2> /dev/null); do
        f="$(echo $l | sed 's,/etc/,/etc/static/,')"
        if [ "$(readlink $l)" = "$f" -a ! -e "$(readlink -f $l)" ]; then
          rm "$l"
        fi
      done
    '';

  };
}