From 24b8e750b5aa3b049a9b570fc60a3cef5acf3418 Mon Sep 17 00:00:00 2001 From: clerie Date: Fri, 20 Oct 2023 23:45:43 +0200 Subject: [PATCH] pkgs/nixfiles: move nixfiles-auto-install to pkg --- flake.nix | 1 + hosts/_iso/configuration.nix | 70 +------------------------ pkgs/nixfiles/nixfiles-auto-install.nix | 9 ++++ pkgs/nixfiles/nixfiles-auto-install.sh | 65 +++++++++++++++++++++++ pkgs/overlay.nix | 1 + 5 files changed, 78 insertions(+), 68 deletions(-) create mode 100644 pkgs/nixfiles/nixfiles-auto-install.nix create mode 100644 pkgs/nixfiles/nixfiles-auto-install.sh diff --git a/flake.nix b/flake.nix index da93c48..91817dc 100644 --- a/flake.nix +++ b/flake.nix @@ -102,6 +102,7 @@ flask-excel iot-data nixfiles-add-secret + nixfiles-auto-install nixfiles-generate-backup-secrets nixfiles-generate-config nixfiles-updated-inputs diff --git a/hosts/_iso/configuration.nix b/hosts/_iso/configuration.nix index 97cab1e..88ccf9c 100644 --- a/hosts/_iso/configuration.nix +++ b/hosts/_iso/configuration.nix @@ -1,72 +1,6 @@ { pkgs, lib, modulesPath, ... }: -let - nixfiles-auto-install = pkgs.writeScriptBin "nixfiles-auto-install" '' - #!${pkgs.bash}/bin/bash - set -euo pipefail - - hostname=host''${RANDOM} - - echo "[I] Deploying with hostname ''${hostname}" - - device="" - for dev in "/dev/vda" "/dev/sda"; do - if [[ -b $dev ]]; then - device=$dev - break - fi - done - - while [[ $# -gt 0 ]]; do - case $1 in - --hostname) - hostname=$2 - shift - shift - ;; - *) - echo "unknown option: $1" - exit 1 - ;; - esac - done - - echo "[I] Formatting disk" - - if [[ -z $device ]]; then - echo "[E] No device to install to" - exit 1 - fi - - echo "[I] Using ''${device}" - - parted --script $device mklabel gpt - parted --script $device disk_set pmbr_boot on - - parted --script $device mkpart boot 0% 512M - parted --script $device set 1 bios_grub on - - parted --script $device mkpart root 512M 100% - - echo "[I] Creating file system" - - mkfs.ext4 -F ''${device}2 - - echo "[I] Mount file system" - - mount ''${device}2 /mnt - - echo "[I] Generate NixOS configuration" - - nixfiles-generate-config --root /mnt --hostname ''${hostname} - - sed -i "s~# boot\.loader\.grub\.device = \"/dev/sda\";~boot\.loader\.grub\.device = \"''${device}\";~g" /mnt/etc/nixos/hosts/''${hostname}/configuration.nix - - echo "[I] Install NixOS" - - nixos-install --flake /mnt/etc/nixos#''${hostname} --root /mnt --no-root-password - ''; -in { +{ imports = [ (modulesPath + "/installer/cd-dvd/installation-cd-base.nix") ]; @@ -74,7 +8,7 @@ in { networking.hostName = "isowo"; isoImage.isoBaseName = "nixos-isowo"; - environment.systemPackages = [ + environment.systemPackages = with pkgs; [ nixfiles-auto-install ]; } diff --git a/pkgs/nixfiles/nixfiles-auto-install.nix b/pkgs/nixfiles/nixfiles-auto-install.nix new file mode 100644 index 0000000..0758aee --- /dev/null +++ b/pkgs/nixfiles/nixfiles-auto-install.nix @@ -0,0 +1,9 @@ +{ pkgs, ... }: + +pkgs.writeShellApplication { + name = "nixfiles-auto-install"; + text = builtins.readFile ./nixfiles-auto-install.sh; + runtimeInputs = with pkgs; [ + nixfiles-generate-config + ]; +} diff --git a/pkgs/nixfiles/nixfiles-auto-install.sh b/pkgs/nixfiles/nixfiles-auto-install.sh new file mode 100644 index 0000000..040201d --- /dev/null +++ b/pkgs/nixfiles/nixfiles-auto-install.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash + +set -euo pipefail + +hostname=host${RANDOM} + +echo "[I] Deploying with hostname ${hostname}" + +device="" +for dev in "/dev/vda" "/dev/sda"; do + if [[ -b $dev ]]; then + device=$dev + break + fi +done + +while [[ $# -gt 0 ]]; do + case $1 in + --hostname) + hostname=$2 + shift + shift + ;; + *) + echo "unknown option: $1" + exit 1 + ;; + esac +done + +echo "[I] Formatting disk" + +if [[ -z $device ]]; then + echo "[E] No device to install to" + exit 1 +fi + +echo "[I] Using ${device}" + +parted --script $device mklabel gpt +parted --script $device disk_set pmbr_boot on + +parted --script $device mkpart boot 0% 512M +parted --script $device set 1 bios_grub on + +parted --script $device mkpart root 512M 100% + +echo "[I] Creating file system" + +mkfs.ext4 -F ${device}2 + +echo "[I] Mount file system" + +mount ${device}2 /mnt + +echo "[I] Generate NixOS configuration" + +nixfiles-generate-config --root /mnt --hostname "${hostname}" + +sed -i "s~# boot\.loader\.grub\.device = \"/dev/sda\";~boot\.loader\.grub\.device = \"${device}\";~g" "/mnt/etc/nixos/hosts/${hostname}/configuration.nix" + +echo "[I] Install NixOS" + +nixos-install --flake "/mnt/etc/nixos#${hostname}" --root /mnt --no-root-password + diff --git a/pkgs/overlay.nix b/pkgs/overlay.nix index c3327ae..44e57ef 100644 --- a/pkgs/overlay.nix +++ b/pkgs/overlay.nix @@ -3,6 +3,7 @@ self: super: { flask-excel = self.python3.pkgs.callPackage ./flask-excel {}; iot-data = self.python3.pkgs.callPackage ./iot-data {}; nixfiles-add-secret = self.callPackage ./nixfiles/nixfiles-add-secret.nix {}; + nixfiles-auto-install = self.callPackage ./nixfiles/nixfiles-auto-install.nix {}; nixfiles-generate-backup-secrets = self.callPackage ./nixfiles/nixfiles-generate-backup-secrets.nix {}; nixfiles-generate-config = self.callPackage ./nixfiles/nixfiles-generate-config.nix {}; nixfiles-updated-inputs = self.callPackage ./nixfiles/nixfiles-updated-inputs.nix {};