summaryrefslogtreecommitdiffstats
path: root/nixos/modules/virtualisation/lxc-container.nix
blob: abdf377c3875e150fcc85d41f27f38680257778a (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
{
  lib,
  config,
  pkgs,
  ...
}:

{
  meta = {
    teams = [ lib.teams.lxc ];
  };

  imports = [
    ./lxc-instance-common.nix

    (lib.mkRemovedOptionModule [
      "virtualisation"
      "lxc"
      "nestedContainer"
    ] "")
    (lib.mkRemovedOptionModule [
      "virtualisation"
      "lxc"
      "privilegedContainer"
    ] "")
  ];

  options = { };

  config =

    {
      boot.isContainer = true;
      systemd.services.register-nix-paths = {
        description = "Register Nix Store Paths";
        unitConfig = {
          DefaultDependencies = false;
          ConditionPathExists = "/nix-path-registration";
        };
        wantedBy = [ "sysinit.target" ];
        before = [
          "sysinit.target"
          "shutdown.target"
          "nix-daemon.socket"
          "nix-daemon.service"
        ];
        after = [ "local-fs.target" ];
        conflicts = [ "shutdown.target" ];
        restartIfChanged = false;
        serviceConfig = {
          Type = "oneshot";
          RemainAfterExit = true;
        };
        script = ''
          ${lib.getExe' config.nix.package.out "nix-store"} --load-db < /nix-path-registration
          rm /nix-path-registration

          # nixos-rebuild also requires a "system" profile
          ${lib.getExe' config.nix.package.out "nix-env"} -p /nix/var/nix/profiles/system --set /run/current-system
        '';
      };

      # supplement 99-ethernet-default-dhcp which excludes veth
      systemd.network = lib.mkIf config.networking.useDHCP {
        networks."99-lxc-veth-default-dhcp" = {
          matchConfig = {
            Type = "ether";
            Kind = "veth";
            Name = [
              "en*"
              "eth*"
            ];
          };
          DHCP = "yes";
          networkConfig.IPv6PrivacyExtensions = "kernel";
        };
      };

      system.nixos.tags = lib.mkOverride 99 [ "lxc" ];
      image.extension = "tar.xz";
      image.filePath = "tarball/${config.image.fileName}";
      system.build.image = lib.mkOverride 99 config.system.build.tarball;

      system.build.tarball = pkgs.callPackage ../../lib/make-system-tarball.nix {
        fileName = config.image.baseName;
        extraArgs = "--owner=0";

        storeContents = [
          {
            object = config.system.build.toplevel;
            symlink = "none";
          }
        ];

        contents = [
          {
            source = config.system.build.toplevel + "/init";
            target = "/sbin/init";
          }
          # Technically this is not required for lxc, but having also make this configuration work with systemd-nspawn.
          # Nixos will setup the same symlink after start.
          {
            source = config.system.build.toplevel + "/etc/os-release";
            target = "/etc/os-release";
          }
        ];

        extraCommands = "mkdir -p proc sys dev";
      };

      system.build.squashfs = pkgs.callPackage ../../lib/make-squashfs.nix {
        fileName = "nixos-lxc-image-${pkgs.stdenv.hostPlatform.system}";

        hydraBuildProduct = true;
        noStrip = true; # keep directory structure
        comp = "zstd -Xcompression-level 6";

        storeContents = [ config.system.build.toplevel ];

        pseudoFiles = [
          "/sbin d 0755 0 0"
          "/sbin/init s 0555 0 0 ${config.system.build.toplevel}/init"
          "/dev d 0755 0 0"
          "/proc d 0555 0 0"
          "/sys d 0555 0 0"
        ];
      };

      system.build.installBootLoader = pkgs.writeScript "install-lxc-sbin-init.sh" ''
        #!${pkgs.runtimeShell}
        ${pkgs.coreutils}/bin/ln -fs "$1/init" /sbin/init
      '';

      # networkd depends on this, but systemd module disables this for containers
      systemd.additionalUpstreamSystemUnits = [ "systemd-udev-trigger.service" ];

      systemd.packages = [ pkgs.distrobuilder.generator ];

    };
}