summaryrefslogtreecommitdiff
path: root/install.nix
blob: f4186c7e6a9e44d5eef77e7c442e5ff4cbea78ed (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
{ config, pkgs, lib, ... }:

with lib;
let
  cfg = config.kexec.justdoit;
in {
  options = {
    kexec.justdoit = {
      rootDevice = mkOption {
        type = types.str;
        default = "/dev/sda";
        description = "the root block device that justdoit will nuke from orbit and force nixos onto";
      };
      bootSize = mkOption {
        type = types.int;
        default = 256;
        description = "size of /boot in mb";
      };
      swapSize = mkOption {
        type = types.int;
        default = 1024;
        description = "size of swap in mb";
      };
    };
  };

  config = lib.mkIf true {
    system.build.justdoit = pkgs.writeScriptBin "justdoit" ''
      #!${pkgs.stdenv.shell}

      set -e

      vgchange -a n

      dd if=/dev/zero of=${cfg.rootDevice} bs=512 count=10000

      sfdisk ${cfg.rootDevice} <<EOF
      label: dos
      device: ${cfg.rootDevice}
      unit: sectors
      ${cfg.rootDevice}1 : size=${toString (2048 * cfg.bootSize)}, type=83
      ${cfg.rootDevice}2 : size=${toString (2048 * cfg.swapSize)}, type=82
      ${cfg.rootDevice}3 : type=83
      EOF
      export ROOT_DEVICE=${cfg.rootDevice}3
      export SWAP_DEVICE=${cfg.rootDevice}2

      mkdir -p /mnt

      mkfs.ext4 ${cfg.rootDevice}1 -L NIXOS_BOOT
      mkswap $SWAP_DEVICE -L NIXOS_SWAP
      mkfs.ext4 $ROOT_DEVICE -L NIXOS_ROOT

      swapon $SWAP_DEVICE
      mount $ROOT_DEVICE /mnt/
      mkdir -p /mnt/boot/
      mount -t ext4 ${cfg.rootDevice}1 /mnt/boot/

      nixos-generate-config --root /mnt/

      hostId=$(echo $(head -c4 /dev/urandom | od -A none -t x4))
      cp ${./target-config.nix} /mnt/etc/nixos/configuration.nix

      cat > /mnt/etc/nixos/generated.nix <<EOF
      { ... }: {
        boot.loader.grub.device = "${cfg.rootDevice}";
        networking.hostId = "$hostId"; # required for zfs use
      }
      EOF

      nixos-install --no-root-passwd -j 4
      reboot
    '';
    environment.systemPackages = [ config.system.build.justdoit ];

    systemd.services.performInstall = {
      requiredBy = [ "multi-user.target" ];

      path = with pkgs; [
        nixStable lvm2 utillinux e2fsprogs
      ] ++ (with config.system.build; [
        nixos-install nixos-generate-config
      ]);
      
      environment.NIX_PATH = lib.concatStringsSep ":" config.nix.nixPath;

      script = "${config.system.build.justdoit}/bin/justdoit";
    };
  };
}