{ description = "A very basic flake"; inputs = { deploy-rs = { url = "github:serokell/deploy-rs"; inputs.nixpkgs.follows = "nixpkgs"; }; image-builder.url = "git+https://git.asonix.dog/asonix/nixos-aarch64-images"; nixpkgs.url = "github:nixos/nixpkgs/master"; sops-nix = { url = "github:Mic92/sops-nix"; inputs.nixpkgs.follows = "nixpkgs"; }; }; outputs = { self, deploy-rs, image-builder, nixpkgs, sops-nix }: let pkgs = import nixpkgs { system = "aarch64-linux"; }; btrbkPrimary = { subvolumes ? [ ] }: { snapshot_dir = "@snapshots"; subvolume = builtins.foldl' (acc: subvol: acc // { ${subvol} = { }; }) { } subvolumes; }; btrbkSecondary = { target-dir, subvolumes ? [ ] }: { target = "send-receive ${target-dir}"; subvolume = builtins.foldl' (acc: subvol: acc // { ${subvol} = { snapshot_dir = "@snapshots"; snapshot_preserve_min = "all"; snapshot_create = "no"; }; }) { } subvolumes; }; nextcloudSubvolumes = [ "@nc-config" "@nc-data" "@postgres" "@redis" "@gitea" "@gitea-conf" "@pihole" "@papermc" "@docker-cfg" "@garage" "@garage-config" ]; postgresSubvolumes = [ "postgres" "postgres-cfg" ]; sharedModule = ({ config, ... }: { services.openssh.settings.PasswordAuthentication = false; # Use the extlinux boot loader. (NixOS wants to enable GRUB by default) boot.loader.grub.enable = false; # Enables the generation of /boot/extlinux/extlinux.conf boot.loader.generic-extlinux-compatible.enable = true; fileSystems."/" = { device = "/dev/disk/by-uuid/44444444-4444-4444-8888-888888888888"; fsType = "ext4"; }; environment.systemPackages = with pkgs; [ bottom btrbk cryptsetup htop ]; sops = { age.keyFile = "/home/asonix/.config/sops/age/keys.txt"; age.generateKey = true; secrets.private_key = { format = "yaml"; sopsFile = ./secrets/btrbk.yaml; }; secrets.btrfsKeyFile = { format = "binary"; sopsFile = ./secrets/keyfile.bin; }; }; services.btrbk = { sshAccess = [ { key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHTqU3EvTgY5/e9m6YyQWypQPK58t9iPmPnPYAvnODGB asonix@lionheart"; roles = [ "source" "info" "send" ]; } ]; extraPackages = with pkgs; [ xz ]; instances.btrbk = { onCalendar = "hourly"; settings = { transaction_log = "/var/log/btrbk.log"; stream_buffer = "512m"; snapshot_preserve_min = "2d"; snapshot_preserve = "35d 20w 12m"; target_preserve_min = "24h"; target_preserve = "24h 7d"; archive_preserve_min = "latest"; archive_preserve = "12m 10y"; ssh_user = "btrbk"; ssh_identity = config.sops.secrets.private_key.path; backend_remote = "btrfs-progs-sudo"; }; }; }; }); makeDockerConfig = { hostname, volume, baseModule, extraModules ? [ ] }: with image-builder.packages.aarch64-linux.modules; nixpkgs.lib.nixosSystem { system = "aarch64-linux"; modules = [ sops-nix.nixosModules.sops baseModule sharedModule userModule ({ config, ... }: { environment.systemPackages = with pkgs; [ docker docker-compose ]; networking.hostName = hostname; virtualisation.docker.enable = true; services.btrbk.instances.btrbk.settings.volume = volume; }) ] ++ extraModules; }; makeNextcloudConfig = { hostname, volume }: with image-builder.packages.aarch64-linux.modules; makeDockerConfig { inherit hostname; inherit volume; baseModule = rockPro64v2; extraModules = [ ({ config, ... }: { environment.etc.crypttab = { enable = true; text = '' cryptdrive1 /dev/sda1 ${config.sops.secrets.btrfsKeyFile.path} luks cryptdrive2 /dev/sdb1 ${config.sops.secrets.btrfsKeyFile.path} luks cryptdrive3 /dev/sdc1 ${config.sops.secrets.btrfsKeyFile.path} luks ''; }; fileSystems."/btrfs/hdd" = { device = "/dev/mapper/cryptdrive1"; fsType = "btrfs"; options = [ "defaults" "compress=zstd" "rw" ]; }; }) ]; }; deployer = { hostname, configuration }: { hostname = hostname; profiles.system = { sshUser = "asonix"; user = "root"; magicRollback = false; sshOpts = [ "-i" "/home/asonix/.ssh/kube-rsa" "-t" ]; path = deploy-rs.lib.aarch64-linux.activate.nixos configuration; }; }; in { nixosConfigurations = with image-builder.packages.aarch64-linux.modules; { nextcloud1 = makeNextcloudConfig { hostname = "nextcloud1"; volume = { # "ssh://192.168.20.28/btrfs/hdd" = btrbkSecondary { # target-dir = "/btrfs/hdd/@snapshots"; # subvolumes = nextcloudSubvolumes; # }; "/btrfs/hdd" = btrbkPrimary { subvolumes = nextcloudSubvolumes; }; }; }; nextcloud2 = makeNextcloudConfig { hostname = "nextcloud2"; volume = { "ssh://192.168.20.21/btrfs/hdd" = btrbkSecondary { target-dir = "/btrfs/hdd/@snapshots"; subvolumes = nextcloudSubvolumes; }; # "/btrfs/hdd" = btrbkPrimary { # subvolumes = nextcloudSubvolumes; # }; }; }; redtail1 = makeDockerConfig { hostname = "redtail1"; volume = { "ssh://192.168.20.24/btrfs/ssd" = btrbkSecondary { target-dir = "/btrfs/ssd/@snapshots"; subvolumes = postgresSubvolumes; }; # "/btrfs/ssd" = btrbkPrimary { # subvolumes = postgresSubvolumes; # }; }; baseModule = rock64; }; redtail2 = makeDockerConfig { hostname = "redtail2"; volume = { # "ssh://192.168.20.23/btrfs/ssd" = btrbkSecondary { # target-dir = "/btrfs/ssd/@snapshots"; # subvolumes = postgresSubvolumes; # }; "/btrfs/ssd" = btrbkPrimary { subvolumes = postgresSubvolumes; }; }; baseModule = rock64; }; whitestorm1 = makeDockerConfig { hostname = "whitestorm1"; volume = { # "ssh://192.168.20.11/btrfs/ssd" = btrbkSecondary { # target-dir = "/btrfs/ssd/@snapshots"; # subvolumes = postgresSubvolumes; # }; "/btrfs/ssd" = btrbkPrimary { subvolumes = postgresSubvolumes; }; }; baseModule = rock64; }; whitestorm2 = makeDockerConfig { hostname = "whitestorm2"; volume = { "ssh://192.168.20.26/btrfs/ssd" = btrbkSecondary { target-dir = "/btrfs/ssd/@snapshots"; subvolumes = postgresSubvolumes; }; # "/btrfs/ssd" = btrbkPrimary { # subvolumes = postgresSubvolumes; # }; }; baseModule = rock64; }; }; deploy.nodes.nextcloud2 = deployer { hostname = "192.168.20.28"; configuration = self.nixosConfigurations.nextcloud2; }; }; }