updates to incorporate cascade installer

This commit is contained in:
James Andariese 2022-09-28 07:08:26 -05:00
parent baf04cc75d
commit 858aa43c40
6 changed files with 111 additions and 23 deletions

View File

@ -0,0 +1,9 @@
with import (toString ../functions);
with builtins;
{pkgs, ...}:
{
config.nixpkgs.overlays = attrValues (import-folder {path = (toString ../overlays);});
}

View File

@ -1,36 +1,53 @@
with builtins;
with import (toString ../functions);
{config, lib, ...}: {
{pkgs, config, lib, ...}: {
options = with lib; with types; {
environment.cascade-source.enable = mkOption {
cascade.source.enable = mkOption {
default = true;
type = bool;
description = ''
Include cascade source code configured for nixos-rebuild.
'';
};
cascade.source.git-url = mkOption {
default = "https://gitlab.com/jamesandariese/cascade";
type = str;
description = ''
git URL where cascade sources can be found
'';
};
cascade.source.set-nix-path = mkOption {
default = true;
type = bool;
description = ''
set cascade=/usr/src/cascade in NIX_PATH if cascade.source.enable is also true.
if this option or cascade.source.enable are false, this will not be done.
'';
};
};
config = with lib; {
system.activationScripts.cascade-source = mkMerge [
(mkIf config.environment.cascade-source.enable ''
(mkIf config.cascade.source.enable ''
if [ ! -f /usr/src/cascade/.created-by-nixos ];then
mkdir -m 0755 -p /usr/src /etc/nixos
[ -h /usr/src/cascade ] && rm /usr/src/cascade # do this first so the dir test that comes next won't read the symlink as a dir
[ -d /usr/src/cascade ] && mv /usr/src/cascade /usr/src/cascade.before-nixos
ln -sfn ${./..} /usr/src/cascade # but why isn't this atomic?
ln -sf /usr/src/cascade/hosts/${config.networking.hostName}.nix /etc/nixos/configuration.nix
[ -e /usr/src/cascade ] && mv /usr/src/cascade /usr/src/cascade.before-nixos
${pkgs.git}/bin/git clone ${config.cascade.source.git-url} /usr/src/cascade
touch /usr/src/cascade/.created-by-nixos
fi
'')
(mkIf (!config.environment.cascade-source.enable) ''
# if we delete the symlink version of cascade, we delete a symlink in /etc/nixos as well
[ -h /usr/src/cascade ] && rm /usr/src/cascade && \
[ -h /etc/nixos/configuration.nix ] && rm /etc/nixos/configuration.nix
(mkIf (!config.cascade.source.enable) ''
# only cleanup if we actually created /usr/src/cascade
if [ -f /usr/src/cascade/.created-by-nixos ];then
rm -rf /usr/src/cascade && \
[ -e /usr/src/cascade.before-nixos ] && mv /usr/src/cascade.before-nixos /usr/src/cascade
fi
'')
];
nix.nixPath = [
"nixos-config=/etc/nixos/configuration.nix"
];
nix.nixPath = mkIf (config.cascade.source.set-nix-path && config.cascade.source.enable) [ "cascade=/usr/src/cascade" ];
};
}

View File

@ -0,0 +1,63 @@
final: prev: {
cascade-installer = prev.writeShellApplication {
name="cascade-installer";
runtimeInputs = with final; [ bash git parted util-linux jq ];
text=''
usage() {
1>&2 echo "usage: cascade-installer HOSTNAME BLOCKDEVTONUKE PROFILE"
}
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ];then
usage
exit 1
fi
HOSTNAME="$1"
BLOCKDEVTONUKE="$2"
PROFILE="$3"
if ! [ -d /usr/src/cascade/profiles/"$PROFILE" ];then
1>&2 echo "profile $PROFILE does not exist. possible options:"
(cd /usr/src/cascade/profiles/ && 1>&2 printf " %s\n" [a-zA-Z0-9]*)
exit 1
fi
mkdir -p /mnt
# if the block device has children...
if lsblk -J "$BLOCKDEVTONUKE" | jq -e '.blockdevices[]|.children' > /dev/null ; then
1>&2 echo "$BLOCKDEVTONUKE still has partitions. please clear the block device before installing"
exit 3
fi
parted "$BLOCKDEVTONUKE" -- mklabel gpt
parted "$BLOCKDEVTONUKE" -- mkpart primary 512MiB -2GiB
parted "$BLOCKDEVTONUKE" -- mkpart primary linux-swap -2GiB 100%
parted "$BLOCKDEVTONUKE" -- mkpart ESP fat32 1MiB 512MiB
parted "$BLOCKDEVTONUKE" -- set 3 esp on
parted "$BLOCKDEVTONUKE" -- name 1 nixos
parted "$BLOCKDEVTONUKE" -- name 2 nixos-swap
parted "$BLOCKDEVTONUKE" -- name 3 ESP
sync
sleep 3
BLK_NIXOS="$(lsblk -OJ "$BLOCKDEVTONUKE" | jq -r '.blockdevices[].children[]|select(.partlabel == "nixos")|.name')"
BLK_SWAP="$(lsblk -OJ "$BLOCKDEVTONUKE" | jq -r '.blockdevices[].children[]|select(.partlabel == "nixos-swap")|.name')"
BLK_ESP="$(lsblk -OJ "$BLOCKDEVTONUKE" | jq -r '.blockdevices[].children[]|select(.partlabel == "ESP")|.name')"
mkfs.xfs -L nixos "$BLK_NIXOS"
mkswap -L nixos-swap "$BLK_SWAP"
mkfs.fat -F 32 -n BOOT "$BLK_ESP"
mount "$BLK_NIXOS" /mnt
mkdir -p /mnt/boot /mnt/etc/nixos
sed -e '
s#[(] *toString ../profiles/.*[)]#(toString /usr/src/cascade/profiles/'"$PROFILE"')#
s#networking.hostName = "nixos";#networking.hostName = "'"$HOSTNAME"'";#
' < /usr/src/cascade/hosts/_basic.nix > /etc/nixos/configuration.nix
swapon "$BLK_SWAP"
nixos-install
'';
};
}

View File

@ -1,4 +1,5 @@
{pkgs ? (import "${import ../../nixpkgs-path.nix}" {}), ...}:
#{pkgs ? (import "${import ../../nixpkgs-path.nix}" {}), ...}:
{pkgs, ...}:
{
imports = [
(toString ../../common)
@ -7,7 +8,7 @@
# used for deployment. This is done automatically with shell.nix.
];
config = {
environment.systemPackages = with pkgs; [ bridge-utils git ];
environment.systemPackages = with pkgs; [ bridge-utils git cascade-installer ];
programs.neovim.enable = true;
programs.neovim.vimAlias = true;
programs.neovim.viAlias = true;
@ -28,6 +29,7 @@
nix.nixPath = with pkgs; [
"nixos-config=/etc/nixos/configuration.nix"
"nixpkgs=${ import ../../nixpkgs-path.nix }"
"home-manager=/usr/src/home-manager"
"morph-options=${morph.lib}/options.nix"

View File

@ -2,7 +2,9 @@
with lib;
{
imports = [ (toString ../qemu-vm) ];
imports = [
(toString ../qemu-vm)
];
config = {
cascade.bridge-interface = mkForce null; # let it come up with its default interface with dhcp first

View File

@ -35,11 +35,6 @@ mkvirt() {
return 1
fi
# free-ips.txt contains fre IPs, one per line
# this is currently not being used in favor of DHCP towards the end
#IPALLOC="$(head -1 free-ips.txt)"
#sed -i -e '1d' free-ips.txt
[ -f "result/nixos.img" ] || (
1>&2 echo "you do not seem to have a result/nixos.img file. building one now."
rebuild-nixos-image || errcho "could not build result/nixos.img." || return $?