Skip to content
Snippets Groups Projects
Commit 41c3a425 authored by Jens Nolte's avatar Jens Nolte
Browse files

Implement 'deploy <machine> iso'

parent aa07be6e
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ readonly cmdname=$(basename $0) ...@@ -9,6 +9,7 @@ readonly cmdname=$(basename $0)
nixos_system_file=$MACHINES_PATH/nixos.nix nixos_system_file=$MACHINES_PATH/nixos.nix
nixos_iso_file=$MACHINES_PATH/nixos_iso.nix
# This script cannot run without the nixos configuration entry point # This script cannot run without the nixos configuration entry point
if [[ ! -f "$nixos_system_file" ]] if [[ ! -f "$nixos_system_file" ]]
...@@ -21,7 +22,7 @@ fi ...@@ -21,7 +22,7 @@ fi
source $DOTFILES_PATH/bin/lib/util.zsh source $DOTFILES_PATH/bin/lib/util.zsh
usage() { usage() {
print "Usage: $cmdname [--via via_hostname] <hostname> [switch|boot|reboot|test|dry-activate|build]" >&2 print "Usage: $cmdname [--via via_hostname] <hostname> [switch|boot|reboot|test|dry-activate|build|iso]" >&2
} }
positional=() positional=()
...@@ -77,7 +78,7 @@ then ...@@ -77,7 +78,7 @@ then
operation="boot" operation="boot"
set_profile=1 set_profile=1
reboot=1 reboot=1
elif [[ "$operation" = "test" || "$operation" = "dry-activate" || "$operation" = "build" ]] elif [[ "$operation" = "test" || "$operation" = "dry-activate" || "$operation" = "build" || "$operation" = "iso" ]]
then then
# pass # pass
else else
...@@ -97,6 +98,19 @@ fi ...@@ -97,6 +98,19 @@ fi
readonly local_temp_dir=$(mktemp --tmpdir --directory phoenix-deploy.XXXXXXXXXX) readonly local_temp_dir=$(mktemp --tmpdir --directory phoenix-deploy.XXXXXXXXXX)
trap "rm -rf $local_temp_dir" EXIT INT HUP TERM trap "rm -rf $local_temp_dir" EXIT INT HUP TERM
if [[ "$operation" = "iso" ]]
then
print_info "Building iso image"
nix build --file "$MACHINES_PATH" --out-link "$local_temp_dir/nixos-iso-$hostname" "nixosIsoDerivations.$hostname"
readonly nixos_iso_path=$(realpath "$local_temp_dir/nixos-iso-$hostname")
print_info "Iso generated"
print $nixos_iso_path
exit 0
fi
print_info "Building target system configuration" print_info "Building target system configuration"
nix build --file "$nixos_system_file" --argstr hostname "$hostname" --out-link "$local_temp_dir/nixos-config-$hostname" nix build --file "$nixos_system_file" --argstr hostname "$hostname" --out-link "$local_temp_dir/nixos-config-$hostname"
readonly nixos_config_path=$(realpath "$local_temp_dir/nixos-config-$hostname") readonly nixos_config_path=$(realpath "$local_temp_dir/nixos-config-$hostname")
......
# This is the entry point for my NixOS configuration. # This is the entry point for my NixOS configuration.
{ name, path, channel }: { name, path, channel, isIso }:
{ lib, config, pkgs, ... }: { lib, config, pkgs, ... }:
let let
...@@ -7,12 +7,11 @@ let ...@@ -7,12 +7,11 @@ let
dotfilesConfig = import (path + "/dotfiles.nix"); dotfilesConfig = import (path + "/dotfiles.nix");
layerImports = map (l: ./layers + "/${l}.nix") dotfilesConfig.layers; layerImports = map (l: ./layers + "/${l}.nix") dotfilesConfig.layers;
in in
{ ({
imports = [ imports = [
./modules ./modules
(path + "/configuration.nix") (path + "/configuration.nix")
(path + "/hardware-configuration.nix") ] ++ layerImports ++ (lib.lists.optional (!isIso) (path + "/hardware-configuration.nix"));
] ++ layerImports;
nixpkgs.config = { nixpkgs.config = {
packageOverrides = ( import ./pkgs ) { inherit lib config; } ; packageOverrides = ( import ./pkgs ) { inherit lib config; } ;
...@@ -21,19 +20,20 @@ in ...@@ -21,19 +20,20 @@ in
# Pin channel in nix path # Pin channel in nix path
nix.nixPath = [ "nixpkgs=${channel}" ]; nix.nixPath = [ "nixpkgs=${channel}" ];
# Default hostname ist machine directory name
networking.hostName = lib.mkDefault name;
} // (lib.attrsets.optionalAttrs (!isIso) {
# Bootloader # Bootloader
boot.loader.systemd-boot.enable = (installResult.bootloader == "efi"); boot.loader.systemd-boot.enable = (installResult.bootloader == "efi");
boot.loader.efi.canTouchEfiVariables = (installResult.bootloader == "efi"); boot.loader.efi.canTouchEfiVariables = (installResult.bootloader == "efi");
boot.loader.grub.enable = (installResult.bootloader == "bios"); boot.loader.grub.enable = (installResult.bootloader == "bios");
boot.loader.grub.device = installResult.installedBlockDevice; boot.loader.grub.device = installResult.installedBlockDevice;
# Default hostname ist machine directory name
networking.hostName = lib.mkDefault name;
boot.initrd.luks.devices = if installResult.luks then { boot.initrd.luks.devices = if installResult.luks then {
cryptvol = { cryptvol = {
device = "/dev/disk/by-uuid/" + installResult.luksPartitionUuid; device = "/dev/disk/by-uuid/" + installResult.luksPartitionUuid;
allowDiscards = true; allowDiscards = true;
}; };
} else {}; } else {};
} }))
...@@ -31,9 +31,9 @@ let ...@@ -31,9 +31,9 @@ let
machinesDirContents = readDir machinesDir; machinesDirContents = readDir machinesDir;
machineNames = filter (p: machinesDirContents.${p} == "directory") (attrNames machinesDirContents); machineNames = filter (p: machinesDirContents.${p} == "directory") (attrNames machinesDirContents);
withMachines = lambda: listToAttrs (map (m: {name = m; value = lambda { name = m; path = (machinesDir + "/${m}"); }; }) machineNames); withMachines = lambda: listToAttrs (map (m: {name = m; value = lambda { name = m; path = (machinesDir + "/${m}"); }; }) machineNames);
mkMachineConfig = { name, path }: ( mkMachineConfig = { name, path, isIso ? false }: (
import ./configuration.nix { import ./configuration.nix {
inherit name path; inherit name path isIso;
channel = machineChannels.${name}; channel = machineChannels.${name};
} }
); );
...@@ -48,10 +48,38 @@ let ...@@ -48,10 +48,38 @@ let
}; };
in in
nixos.system; nixos.system;
mkNixosIsoDerivation = { name, path }:
let
channel = machineChannels.${name};
configuration = { config, ... }:
{
imports = [
(mkMachineConfig { inherit name path; isIso = true; })
<nixpkgs/nixos/modules/installer/cd-dvd/iso-image.nix>
<nixpkgs/nixos/modules/profiles/all-hardware.nix>
<nixpkgs/nixos/modules/profiles/base.nix>
];
isoImage.isoName = "${config.isoImage.isoBaseName}-${config.system.nixos.label}-isohost-${name}.iso";
isoImage.volumeID = substring 0 11 "NIXOS_ISO";
isoImage.makeEfiBootable = true;
isoImage.makeUsbBootable = true;
boot.loader.grub.memtest86.enable = true;
};
# Importing <nixpkgs/nixos> results in a nixos system closure
nixos = import "${channel}/nixos" {
system = "x86_64-linux";
inherit configuration;
};
in
nixos.config.system.build.isoImage;
in in
{ {
configurations = withMachines mkMachineConfig; configurations = withMachines mkMachineConfig;
nixosSystemDerivations = withMachines mkNixosSystemDerivation; nixosSystemDerivations = withMachines mkNixosSystemDerivation;
nixosIsoDerivations = withMachines mkNixosIsoDerivation;
machineTemplates = withMachines ({name, path}: import (path + /template.nix)); machineTemplates = withMachines ({name, path}: import (path + /template.nix));
channels = machineChannels; channels = machineChannels;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment