]> git.immae.eu Git - perso/Immae/Config/Nixies.git/blame - vms/images.nix
Add some fixes to stdenv prefix hook
[perso/Immae/Config/Nixies.git] / vms / images.nix
CommitLineData
8eb07d94
IB
1let
2 pkgs = import <nixpkgs> {};
3 lib = pkgs.lib;
4 toEval = modules:
5 import <nixpkgs/nixos/lib/eval-config.nix> {
6 system = pkgs.system;
7 modules = [ ./configuration.nix ] ++ modules;
8 };
9 modules = {
10 docker = [
11 {
12 config = {
13 boot.isContainer = true;
14 system.activationScripts.installInitScript = ''
15 ln -fs $systemConfig/init /init
16 '';
17 };
18 }
19 ];
20 light = [
21 { config.virtualisation.graphics = false; }
22 <nixpkgs/nixos/modules/virtualisation/qemu-vm.nix>
23 ];
24 standalone = [
25 {
26 config = {
27 fileSystems."/" = {
28 device = "/dev/disk/by-label/nixos";
29 fsType = "ext4";
30 autoResize = true;
31 };
32
33 boot = {
34 kernelParams = [ "console=ttyS0" ];
35 loader = {
36 timeout = 0;
37 grub.device = "/dev/xvda";
38 grub.configurationLimit = 0;
39 };
40
41 initrd = {
42 network.enable = true;
43 };
44 };
45
46 services.udisks2.enable = false;
47 };
48 }
49 ];
50 };
51 evals = {
52 light = (toEval modules.light).config.system.build.vm;
53 docker =
54 # inspired from
55 # https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/virtualisation/docker-image.nix
56 let
57 eval = toEval modules.docker;
58 in
59 pkgs.callPackage <nixpkgs/nixos/lib/make-system-tarball.nix> {
60 contents = [
61 {
62 source = "${eval.config.system.build.toplevel}/.";
63 target = "./";
64 }
65 ];
66 extraArgs = "--owner=0";
67
68 # Add init script to image
69 storeContents = map (x: { object = x; symlink = "none"; }) [
70 eval.config.system.build.toplevel
71 pkgs.stdenv
72 ];
73
74 # Some container managers like lxc need these
75 extraCommands = "mkdir -p proc sys dev";
76 };
77 standalone =
78 let
79 eval = toEval modules.standalone;
80 name = "nixos-${eval.config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
81 in
82 import <nixpkgs/nixos/lib/make-disk-image.nix> {
83 inherit lib name pkgs;
84 config = eval.config;
85 contents = [];
86 diskSize = 2048;
87 format = "qcow2";
88 postVM = ''
89 extension=''${diskImage##*.}
90 friendlyName=$out/${name}.$extension
91 mv "$diskImage" "$friendlyName"
92 diskImage=$friendlyName
93
94 mkdir -p $out/nix-support
95
96 ${pkgs.jq}/bin/jq -n \
97 --arg label ${lib.escapeShellArg eval.config.system.nixos.label} \
98 --arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \
99 --arg logical_bytes "$(${pkgs.qemu}/bin/qemu-img info --output json "$diskImage" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
100 --arg file "$diskImage" \
101 '$ARGS.named' \
102 > $out/nix-support/image-info.json
103 '';
104 };
105 };
106 scripts = {
107 standalone = pkgs.writeScript "run" ''
108 #!${pkgs.stdenv.shell}
109
110 file=$(cat ${evals.standalone}/nix-support/image-info.json | jq -r .file)
111 cp $file ./nixos.qcow2
112 chmod u+w nixos.qcow2
113
114 trap "rm -f nixos.qcow2" EXIT
115 ${pkgs.qemu}/bin/qemu-system-x86_64 -nographic --cpu host --enable-kvm -hda nixos.qcow2
116 '';
117 light = pkgs.writeScript "run" ''
118 #!${pkgs.stdenv.shell}
119
120 trap "rm -f nixos.qcow2" EXIT
121 ${evals.light}/bin/run-nixos-vm
122 '';
123 docker = pkgs.writeScript "run" ''
124 #!${pkgs.stdenv.shell}
125
126 docker import ${evals.docker}/tarball/nixos-system-*.tar.xz nixos-docker
127 cid=$(docker run --rm --privileged --detach nixos-docker /init)
128 trap "docker stop $cid" EXIT
129 sleep 10
130 docker exec -it $cid /run/current-system/sw/bin/bash
131 '';
132 };
133in
134 lib.mapAttrs (name: v: v // { run = scripts.${name}; eval = evals.${name}; modules = modules.${name};}) scripts