summaryrefslogtreecommitdiff
path: root/vms/images.nix
blob: fd6c967999969ffc6cd05a15dc397c191363d977 (plain) (blame)
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
let
  pkgs = import <nixpkgs> {};
  lib = pkgs.lib;
  toEval = modules:
    import <nixpkgs/nixos/lib/eval-config.nix> {
      system = pkgs.system;
      modules = [ ./configuration.nix ] ++ modules;
    };
  modules = {
    docker = [
      {
        config = {
          boot.isContainer = true;
          system.activationScripts.installInitScript = ''
            ln -fs $systemConfig/init /init
          '';
        };
      }
    ];
    light = [
      { config.virtualisation.graphics = false; }
      <nixpkgs/nixos/modules/virtualisation/qemu-vm.nix>
    ];
    standalone = [
      {
        config = {
          fileSystems."/" = {
            device = "/dev/disk/by-label/nixos";
            fsType = "ext4";
            autoResize = true;
          };

          boot = {
            kernelParams = [ "console=ttyS0" ];
            loader = {
              timeout = 0;
              grub.device = "/dev/xvda";
              grub.configurationLimit = 0;
            };

            initrd = {
              network.enable = true;
            };
          };

          services.udisks2.enable = false;
        };
      }
    ];
  };
  evals = {
    light = (toEval modules.light).config.system.build.vm;
    docker =
      # inspired from
      # https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/virtualisation/docker-image.nix
      let
        eval = toEval modules.docker;
      in
        pkgs.callPackage <nixpkgs/nixos/lib/make-system-tarball.nix> {
          contents = [
            {
              source = "${eval.config.system.build.toplevel}/.";
              target = "./";
            }
          ];
          extraArgs = "--owner=0";

          # Add init script to image
          storeContents = map (x: { object = x; symlink = "none"; }) [
            eval.config.system.build.toplevel
            pkgs.stdenv
          ];

          # Some container managers like lxc need these
          extraCommands = "mkdir -p proc sys dev";
        };
    standalone =
      let
        eval = toEval modules.standalone;
        name = "nixos-${eval.config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
      in
        import <nixpkgs/nixos/lib/make-disk-image.nix> {
          inherit lib name pkgs;
          config = eval.config;
          contents = [];
          diskSize = 2048;
          format = "qcow2";
          postVM = ''
            extension=''${diskImage##*.}
            friendlyName=$out/${name}.$extension
            mv "$diskImage" "$friendlyName"
            diskImage=$friendlyName

            mkdir -p $out/nix-support

            ${pkgs.jq}/bin/jq -n \
              --arg label ${lib.escapeShellArg eval.config.system.nixos.label} \
              --arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \
              --arg logical_bytes "$(${pkgs.qemu}/bin/qemu-img info --output json "$diskImage" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
              --arg file "$diskImage" \
              '$ARGS.named' \
              > $out/nix-support/image-info.json
          '';
        };
  };
  scripts = {
    standalone = pkgs.writeScript "run" ''
      #!${pkgs.stdenv.shell}

      file=$(cat ${evals.standalone}/nix-support/image-info.json | jq -r .file)
      cp $file ./nixos.qcow2
      chmod u+w nixos.qcow2

      trap "rm -f nixos.qcow2" EXIT
      ${pkgs.qemu}/bin/qemu-system-x86_64 -nographic --cpu host --enable-kvm -hda nixos.qcow2
    '';
    light = pkgs.writeScript "run" ''
      #!${pkgs.stdenv.shell}

      trap "rm -f nixos.qcow2" EXIT
      ${evals.light}/bin/run-nixos-vm
    '';
    docker = pkgs.writeScript "run" ''
      #!${pkgs.stdenv.shell}

      docker import ${evals.docker}/tarball/nixos-system-*.tar.xz nixos-docker
      cid=$(docker run --rm --privileged --detach nixos-docker /init)
      trap "docker stop $cid" EXIT
      sleep 10
      docker exec -it $cid /run/current-system/sw/bin/bash
    '';
  };
in
  lib.mapAttrs (name: v: v // { run = scripts.${name}; eval = evals.${name}; modules = modules.${name};}) scripts