2 pkgs = import <nixpkgs> {};
5 import <nixpkgs/nixos/lib/eval-config.nix> {
7 modules = [ ./configuration.nix ] ++ modules;
13 boot.isContainer = true;
14 system.activationScripts.installInitScript = ''
15 ln -fs $systemConfig/init /init
21 { config.virtualisation.graphics = false; }
22 <nixpkgs/nixos/modules/virtualisation/qemu-vm.nix>
28 device = "/dev/disk/by-label/nixos";
34 kernelParams = [ "console=ttyS0" ];
37 grub.device = "/dev/xvda";
38 grub.configurationLimit = 0;
42 network.enable = true;
46 services.udisks2.enable = false;
52 light = (toEval modules.light).config.system.build.vm;
55 # https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/virtualisation/docker-image.nix
57 eval = toEval modules.docker;
59 pkgs.callPackage <nixpkgs/nixos/lib/make-system-tarball.nix> {
62 source = "${eval.config.system.build.toplevel}/.";
66 extraArgs = "--owner=0";
68 # Add init script to image
69 storeContents = map (x: { object = x; symlink = "none"; }) [
70 eval.config.system.build.toplevel
74 # Some container managers like lxc need these
75 extraCommands = "mkdir -p proc sys dev";
79 eval = toEval modules.standalone;
80 name = "nixos-${eval.config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
82 import <nixpkgs/nixos/lib/make-disk-image.nix> {
83 inherit lib name pkgs;
89 extension=''${diskImage##*.}
90 friendlyName=$out/${name}.$extension
91 mv "$diskImage" "$friendlyName"
92 diskImage=$friendlyName
94 mkdir -p $out/nix-support
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" \
102 > $out/nix-support/image-info.json
107 standalone = pkgs.writeScript "run" ''
108 #!${pkgs.stdenv.shell}
110 file=$(cat ${evals.standalone}/nix-support/image-info.json | jq -r .file)
111 cp $file ./nixos.qcow2
112 chmod u+w nixos.qcow2
114 trap "rm -f nixos.qcow2" EXIT
115 ${pkgs.qemu}/bin/qemu-system-x86_64 -nographic --cpu host --enable-kvm -hda nixos.qcow2
117 light = pkgs.writeScript "run" ''
118 #!${pkgs.stdenv.shell}
120 trap "rm -f nixos.qcow2" EXIT
121 ${evals.light}/bin/run-nixos-vm
123 docker = pkgs.writeScript "run" ''
124 #!${pkgs.stdenv.shell}
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
130 docker exec -it $cid /run/current-system/sw/bin/bash
134 lib.mapAttrs (name: v: v // { run = scripts.${name}; eval = evals.${name}; modules = modules.${name};}) scripts