Extract shared AMI NixOS modules

This commit is contained in:
Timothy J. Aveni
2026-06-30 19:10:56 -07:00
parent efbf70d0ef
commit ab223837c4
+34 -62
View File
@@ -9,7 +9,7 @@ ready_marker="$state_dir/ami-ready"
mkdir -p "$state_dir" mkdir -p "$state_dir"
write_bootstrap_config() { write_config() {
mkdir -p /etc/nixos mkdir -p /etc/nixos
cat > /etc/nixos/flake.nix <<'EOF' cat > /etc/nixos/flake.nix <<'EOF'
{ {
@@ -17,14 +17,11 @@ write_bootstrap_config() {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
}; };
outputs = { nixpkgs, ... }: { outputs = { nixpkgs, ... }:
nixosConfigurations.ami-builder-bootstrap = nixpkgs.lib.nixosSystem { let
system = "${system}"; baseModule = { lib, modulesPath, pkgs, ... }: {
modules = [
({ config, lib, modulesPath, pkgs, ... }: {
imports = [ "$${modulesPath}/virtualisation/amazon-image.nix" ]; imports = [ "$${modulesPath}/virtualisation/amazon-image.nix" ];
networking.hostName = "nixos-zfs-ec2-builder";
networking.hostId = "6e69787a"; networking.hostId = "6e69787a";
nix.settings.experimental-features = [ "nix-command" "flakes" ]; nix.settings.experimental-features = [ "nix-command" "flakes" ];
@@ -38,11 +35,18 @@ write_bootstrap_config() {
]; ];
environment.systemPackages = [ environment.systemPackages = [
pkgs.coreutils
pkgs.git pkgs.git
pkgs.rsync pkgs.rsync
pkgs.systemd
pkgs.zfs pkgs.zfs
]; ];
programs.bash.shellAliases = {
nixrb = "sudo nix --refresh flake update --flake /etc/nixos && sudo nixos-rebuild switch --flake /etc/nixos#nixos-zfs-ec2";
nixfu = "sudo nix --refresh flake update --flake /etc/nixos";
};
services.amazon-ssm-agent.enable = true; services.amazon-ssm-agent.enable = true;
users.users.builder-admin = { users.users.builder-admin = {
@@ -53,42 +57,11 @@ write_bootstrap_config() {
security.sudo.wheelNeedsPassword = false; security.sudo.wheelNeedsPassword = false;
system.stateVersion = "25.05"; system.stateVersion = "25.05";
}) };
];
};
};
}
EOF
}
write_final_config() { zfsNixModule = { lib, ... }: {
mkdir -p /etc/nixos
cat > /etc/nixos/flake.nix <<'EOF'
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = { nixpkgs, ... }: {
nixosConfigurations.nixos-zfs-ec2 = nixpkgs.lib.nixosSystem {
system = "${system}";
modules = [
({ config, lib, modulesPath, pkgs, ... }: {
imports = [ "$${modulesPath}/virtualisation/amazon-image.nix" ];
networking.hostName = "nixos-zfs-ec2";
networking.hostId = "6e69787a";
nix.settings.experimental-features = [ "nix-command" "flakes" ];
boot.supportedFilesystems = [ "zfs" ];
boot.zfs.extraPools = [ "${nix_pool}" ]; boot.zfs.extraPools = [ "${nix_pool}" ];
boot.zfs.forceImportRoot = false; systemd.services.amazon-init.enable = lib.mkForce false;
swapDevices = [
{
device = "/swapfile";
size = 4096;
}
];
fileSystems."/nix" = { fileSystems."/nix" = {
device = "${nix_pool}/nix"; device = "${nix_pool}/nix";
@@ -105,16 +78,9 @@ write_final_config() {
after = [ "nix.mount" ]; after = [ "nix.mount" ];
requires = [ "nix.mount" ]; requires = [ "nix.mount" ];
}; };
};
environment.systemPackages = [ userDataApplyModule = { config, lib, pkgs, ... }: {
pkgs.git
pkgs.rsync
pkgs.zfs
];
services.amazon-ssm-agent.enable = true;
systemd.services.amazon-init.enable = lib.mkForce false;
systemd.services.nixos-user-data-flake = { systemd.services.nixos-user-data-flake = {
description = "Apply NixOS flake from EC2 user data"; description = "Apply NixOS flake from EC2 user data";
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
@@ -166,25 +132,31 @@ RUNNER_EOF
"$${pkgs.runtimeShell}" "$runner" "$${pkgs.runtimeShell}" "$runner"
''; '';
}; };
};
users.users.builder-admin = { mkHost = hostName: extraModules: nixpkgs.lib.nixosSystem {
isNormalUser = true; system = "${system}";
extraGroups = [ "wheel" ]; modules = [
openssh.authorizedKeys.keys = ${ssh_keys}; baseModule
}; ({ ... }: {
security.sudo.wheelNeedsPassword = false; networking.hostName = hostName;
})
system.stateVersion = "25.05"; ] ++ extraModules;
}) };
in
{
nixosConfigurations.ami-builder-bootstrap = mkHost "nixos-zfs-ec2-builder" [];
nixosConfigurations.nixos-zfs-ec2 = mkHost "nixos-zfs-ec2" [
zfsNixModule
userDataApplyModule
]; ];
}; };
};
} }
EOF EOF
} }
if [ ! -e "$bootstrap_marker" ]; then if [ ! -e "$bootstrap_marker" ]; then
write_bootstrap_config write_config
nixos-rebuild boot --flake /etc/nixos#ami-builder-bootstrap nixos-rebuild boot --flake /etc/nixos#ami-builder-bootstrap
touch "$bootstrap_marker" touch "$bootstrap_marker"
systemctl reboot systemctl reboot
@@ -224,7 +196,7 @@ if ! mountpoint -q /mnt/nixos-zfs-nix; then
mount -t zfs "$nix_pool/nix" /mnt/nixos-zfs-nix mount -t zfs "$nix_pool/nix" /mnt/nixos-zfs-nix
fi fi
write_final_config write_config
nixos-rebuild boot --flake /etc/nixos#nixos-zfs-ec2 nixos-rebuild boot --flake /etc/nixos#nixos-zfs-ec2
rsync -aHAX --numeric-ids \ rsync -aHAX --numeric-ids \