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