summaryrefslogtreecommitdiff
path: root/install.nix
diff options
context:
space:
mode:
authortilpner2018-04-19 22:38:13 +0200
committertilpner2018-04-19 22:38:13 +0200
commitc52f9cb05ba3fd6533543d691de778091a30e2e1 (patch)
treeb3a01d2c338be776dbdb63cfe867a8b691c31765 /install.nix
downloadnixos-kexec-master.tar.gz
nixos-kexec-master.tar.xz
nixos-kexec-master.zip
Initial commitHEADmaster
Diffstat (limited to 'install.nix')
-rw-r--r--install.nix90
1 files changed, 90 insertions, 0 deletions
diff --git a/install.nix b/install.nix
new file mode 100644
index 0000000..f4186c7
--- /dev/null
+++ b/install.nix
@@ -0,0 +1,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";
+ };
+ };
+}