1
0

pkgs/nixfiles: move nixfiles-auto-install to pkg

This commit is contained in:
clerie 2023-10-20 23:45:43 +02:00
parent b9af028fa7
commit 24b8e750b5
5 changed files with 78 additions and 68 deletions

View File

@ -102,6 +102,7 @@
flask-excel flask-excel
iot-data iot-data
nixfiles-add-secret nixfiles-add-secret
nixfiles-auto-install
nixfiles-generate-backup-secrets nixfiles-generate-backup-secrets
nixfiles-generate-config nixfiles-generate-config
nixfiles-updated-inputs nixfiles-updated-inputs

View File

@ -1,72 +1,6 @@
{ pkgs, lib, modulesPath, ... }: { 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 = [ imports = [
(modulesPath + "/installer/cd-dvd/installation-cd-base.nix") (modulesPath + "/installer/cd-dvd/installation-cd-base.nix")
]; ];
@ -74,7 +8,7 @@ in {
networking.hostName = "isowo"; networking.hostName = "isowo";
isoImage.isoBaseName = "nixos-isowo"; isoImage.isoBaseName = "nixos-isowo";
environment.systemPackages = [ environment.systemPackages = with pkgs; [
nixfiles-auto-install nixfiles-auto-install
]; ];
} }

View File

@ -0,0 +1,9 @@
{ pkgs, ... }:
pkgs.writeShellApplication {
name = "nixfiles-auto-install";
text = builtins.readFile ./nixfiles-auto-install.sh;
runtimeInputs = with pkgs; [
nixfiles-generate-config
];
}

View File

@ -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

View File

@ -3,6 +3,7 @@ self: super: {
flask-excel = self.python3.pkgs.callPackage ./flask-excel {}; flask-excel = self.python3.pkgs.callPackage ./flask-excel {};
iot-data = self.python3.pkgs.callPackage ./iot-data {}; iot-data = self.python3.pkgs.callPackage ./iot-data {};
nixfiles-add-secret = self.callPackage ./nixfiles/nixfiles-add-secret.nix {}; 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-backup-secrets = self.callPackage ./nixfiles/nixfiles-generate-backup-secrets.nix {};
nixfiles-generate-config = self.callPackage ./nixfiles/nixfiles-generate-config.nix {}; nixfiles-generate-config = self.callPackage ./nixfiles/nixfiles-generate-config.nix {};
nixfiles-updated-inputs = self.callPackage ./nixfiles/nixfiles-updated-inputs.nix {}; nixfiles-updated-inputs = self.callPackage ./nixfiles/nixfiles-updated-inputs.nix {};