{ description = "C3D2 NixOS configurations"; inputs = { nixpkgs.url = "github:nixos/nixpkgs/release-21.11"; nixpkgs-mobilizon.url = "github:minijackson/nixpkgs/mobilizon"; nixpkgs-openwebrx.url = "github:astro/nixpkgs/openwebrx"; nixpkgs-unstable.url = "github:nixos/nixpkgs"; secrets.url = "git+ssh://gitea@gitea.c3d2.de/c3d2-admins/secrets.git"; secrets.inputs.sops-nix.follows = "sops-nix"; nixos-hardware.url = "github:nixos/nixos-hardware"; zentralwerk.url = "git+https://gitea.c3d2.de/zentralwerk/network.git"; yammat.url = "git+https://gitea.c3d2.de/astro/yammat.git?ref=nix"; yammat.inputs.nixpkgs.follows = "nixpkgs"; scrapers.url = "git+https://gitea.c3d2.de/astro/scrapers.git"; scrapers.flake = false; spacemsg.url = "github:astro/spacemsg"; spacemsg.flake = false; tigger.url = "github:astro/tigger"; tigger.flake = false; ticker.url = "git+https://gitea.c3d2.de/astro/ticker.git"; ticker.flake = false; heliwatch.url = "git+https://gitea.c3d2.de/astro/heliwatch.git"; heliwatch.inputs.nixpkgs.follows = "nixpkgs"; sops-nix.url = "github:Mic92/sops-nix"; naersk.url = "github:mhuesch/naersk"; naersk.inputs.nixpkgs.follows = "nixpkgs"; # TODO: https://github.com/nix-community/fenix/pull/68 # fenix.url = "github:nix-community/fenix"; fenix.url = "github:rvolosatovs/fenix/fix/rustc-patch"; fenix.inputs.nixpkgs.follows = "nixpkgs"; }; outputs = inputs@{ self, nixpkgs, secrets, nixos-hardware, zentralwerk, yammat, scrapers, spacemsg, tigger, ticker, heliwatch, sops-nix, naersk, fenix, ... }: let inherit (nixpkgs) lib; forAllSystems = lib.genAttrs [ "aarch64-linux" "x86_64-linux" ]; extractZwHosts = { hosts4, hosts6, ... }: lib.recursiveUpdate ( builtins.foldl' (result: name: lib.recursiveUpdate result { "${name}".ip4 = hosts4."${name}"; } ) {} (builtins.attrNames hosts4) ) ( builtins.foldl' (result: ctx: builtins.foldl' (result: name: lib.recursiveUpdate result { "${name}".ip6 = hosts6."${ctx}"."${name}"; } ) result (builtins.attrNames hosts6."${ctx}") ) {} (builtins.attrNames hosts6) ); zwHostRegistry = { hosts = builtins.foldl' (result: net: lib.recursiveUpdate result (extractZwHosts zentralwerk.lib.config.site.net."${net}") ) {} [ "core" "c3d2" "serv" ]; }; extraHostRegistry.hosts = import ./host-registry.nix; hostRegistry = lib.recursiveUpdate zwHostRegistry extraHostRegistry; getHostAddr = name: let hostConf = hostRegistry.hosts."${name}"; in if hostConf ? ip4 then hostConf.ip4 else if hostConf ? ip6 then hostConf.ip6 else throw "Host ${name} has no ip4 or ip6 address"; in { overlay = import ./overlay; legacyPackages = lib.attrsets.mapAttrs (system: pkgs: pkgs.appendOverlays [ fenix.overlay naersk.overlay self.overlay ]) nixpkgs.legacyPackages; packages = lib.attrsets.mapAttrs (system: pkgs: let overlayPkgs = builtins.intersectAttrs (self.overlay {} {}) pkgs; in overlayPkgs // { list-upgradable = pkgs.writeScriptBin "list-upgradable" '' #! ${pkgs.runtimeShell} NORMAL="\033[0m" RED="\033[0;31m" YELLOW="\033[0;33m" GREEN="\033[0;32m" ${pkgs.lib.concatMapStringsSep "\n" (name: let addr = getHostAddr name; in nixpkgs.lib.optionalString (addr != null) '' echo -n -e "${name}: $RED" RUNNING=$(ssh -o PreferredAuthentications=publickey -o StrictHostKeyChecking=accept-new root@"${addr}" "readlink /run/current-system") if [ $? = 0 ] && [ -n "$RUNNING" ]; then CURRENT=$(nix eval --raw ".#nixosConfigurations.${name}.config.system.build.toplevel" 2>/dev/null) RUNNING_VER=$(basename $RUNNING|rev|cut -d - -f 1|rev) RUNNING_DATE=$(echo $RUNNING_VER|cut -d . -f 3) CURRENT_VER=$(basename $CURRENT|rev|cut -d - -f 1|rev) CURRENT_DATE=$(echo $CURRENT_VER|cut -d . -f 3) if [ "$RUNNING" = "$CURRENT" ]; then echo -e "$GREEN"current"$NORMAL $RUNNING_VER" elif [ $RUNNING_DATE -gt $CURRENT_DATE ]; then echo -e "$GREEN"newer"$NORMAL $RUNNING_VER > $CURRENT_VER" elif [ "$RUNNING_VER" = "$CURRENT_VER" ]; then echo -e "$YELLOW"modified"$NORMAL $RUNNING_VER" elif [ -n "$RUNNING_VER" ]; then echo -e "$RED"outdated"$NORMAL $RUNNING_VER < $CURRENT_VER" else echo -e "$RED"error"$NORMAL $RUNNING_VER" fi fi echo -n -e "$NORMAL" '') (builtins.attrNames self.nixosConfigurations)} ''; prebuild-all = pkgs.runCommandNoCC "prebuild-all" { preferLocalBuild = true; } '' mkdir $out ${pkgs.lib.concatMapStrings (name: '' ln -s ${self.nixosConfigurations."${name}".config.system.build.toplevel} name '') (builtins.attrNames self.nixosConfigurations)} ''; prebuild-all-remote = pkgs.writeScriptBin "prebuild-all" '' #!${pkgs.runtimeShell} -e nix copy --to ssh://$1 ${secrets} nix copy --to ssh://$1 ${self} set -x ssh $1 -- nix build -vL --no-link ${ pkgs.lib.concatMapStringsSep " " (name: "${self}#nixosConfigurations.${name}.config.system.build.toplevel" ) (builtins.attrNames self.nixosConfigurations) } ''; } // builtins.foldl' (result: host: result // { # TODO: check if the ethernet address is reachable and if not, # execute wol on a machine in HQ. "${host}-wake" = pkgs.writeScriptBin "${host}-wake" '' #!${pkgs.runtimeShell} exec ${pkgs.wol}/bin/wol ${hostRegistry.hosts."${host}".ether} ''; }) {} (builtins.attrNames (nixpkgs.lib.filterAttrs (_: { wol ? false, ... }: wol) hostRegistry.hosts)) // builtins.foldl' (result: name: let host = getHostAddr name; target = ''root@"${host}"''; rebuildArg = "--flake ${self}#${name}"; profile = self.nixosConfigurations."${name}".config.system.build.toplevel; in result // { # Generate a small script for copying this flake to the # remote machine and bulding and switching there. # Can be run with `nix run c3d2#…-nixos-rebuild switch` "${name}-nixos-rebuild" = pkgs.writeScriptBin "${name}-nixos-rebuild" '' #!${pkgs.runtimeShell} -ex [[ $(ssh ${target} cat /etc/hostname) == ${name} ]] nix copy --to ssh://${target} ${secrets} nix copy --to ssh://${target} ${self} ssh ${target} nixos-rebuild ${rebuildArg} "$@" ''; "${name}-nixos-rebuild-local" = pkgs.writeScriptBin "${name}-nixos-rebuild" '' #!${pkgs.runtimeShell} -ex [[ $(ssh ${target} cat /etc/hostname) == ${name} ]] nix copy --to ssh://${target} ${profile} ssh ${target} "${profile}/bin/switch-to-configuration $*" ''; "${name}-cleanup" = pkgs.writeScriptBin "${name}-cleanup" '' #!${pkgs.runtimeShell} -ex ssh ${target} "time nix-collect-garbage -d && time nix-store --optimise" ''; }) {} (builtins.attrNames self.nixosConfigurations) // builtins.foldl' (result: host: let inherit (self.nixosConfigurations.${host}) config; vmConfig = (nixpkgs.lib.nixosSystem { modules = [ { inherit config; } ]; }).config; in result // nixpkgs.lib.optionalAttrs (config.system.build ? sdImage) { "${host}-sdImage" = config.system.build.sdImage; } // # if you want to try your containers in a vm, turn off # `boot.isContainer` just for testing. nixpkgs.lib.optionalAttrs (config.system.build ? vm && !config.boot.isContainer) { "${host}-vm" = vmConfig.system.build.vm .overrideAttrs (oa: { meta.mainProgram = "run-${host}-vm"; }); } // nixpkgs.lib.optionalAttrs config.boot.loader.raspberryPi.enable { "${host}-boot" = pkgs.runCommand "tftproot" {} '' mkdir -p $out cp -rs ${pkgs.raspberrypifw}/share/raspberrypi/boot/* $out/ rm -f $out/kernel.img $out/initrd.img ln -s ${config.system.build.kernel}/Image $out/kernel.img ln -s ${config.system.build.initialRamdisk}/initrd $out/initrd.img cat << EOF > $out/config.txt kernel kernel.img initramfs initrd.img followkernel EOF echo "dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 verbose init=${config.system.build.toplevel}/init" > $out/cmdline.txt ''; } ) {} (builtins.attrNames self.nixosConfigurations) ) self.legacyPackages; nixosConfigurations = let nixosSystem' = # Our custom NixOS builder { nixpkgs ? inputs.nixpkgs, modules, extraArgs ? {}, system ? "x86_64-linux" }: nixpkgs.lib.nixosSystem { inherit system; modules = [ ({ pkgs, ... }: { _module.args = extraArgs // { inherit hostRegistry inputs zentralwerk; }; nixpkgs.overlays = [ self.overlay ]; }) self.nixosModules.c3d2 ./config/audio-server ./config/c3d2.nix ./config/cache.nix ./config/ceph-storage.nix ./config/stats.nix ./modules/pi-sensors.nix ] ++ modules; }; in { freifunk = nixosSystem' { modules = [ ./hosts/containers/freifunk { nixpkgs.overlays = with secrets.overlays; [ freifunk ospf ]; } ]; }; gitea = nixosSystem' { nixpkgs = inputs.nixpkgs-unstable; modules = [ ./config/lxc-container.nix ./hosts/containers/gitea ]; }; glotzbert = nixosSystem' { modules = [ ./hosts/glotzbert nixos-hardware.nixosModules.common-cpu-intel nixos-hardware.nixosModules.common-pc-ssd secrets.nixosModules.admins { sops.defaultSopsFile = "${secrets}/hosts/glotzbert/secrets.yaml"; } ]; }; nix-build = nixosSystem' { modules = [ ./hosts/containers/nix-build ]; }; pulsebert = nixosSystem' { modules = [ "${inputs.nixpkgs}/nixos/modules/installer/sd-card/sd-image-aarch64.nix" ./hosts/pulsebert ]; system = "aarch64-linux"; }; radiobert = nixosSystem' { modules = [ ({ modulesPath, ... }: { imports = [ "${modulesPath}/installer/sd-card/sd-image-aarch64.nix" ]; nixpkgs.overlays = [ heliwatch.overlay ]; sops.defaultSopsFile = "${secrets}/hosts/radiobert/secrets.yaml"; }) ./hosts/radiobert ]; system = "aarch64-linux"; }; nncp = nixosSystem' { nixpkgs = inputs.nixpkgs-unstable; modules = [ self.nixosModules.nncp ./config/lxc-container.nix ./hosts/containers/nncp ]; }; dacbert = nixosSystem' { modules = [ "${inputs.nixpkgs}/nixos/modules/installer/sd-card/sd-image-aarch64.nix" nixos-hardware.nixosModules.raspberry-pi-4 ./hosts/dacbert ]; system = "aarch64-linux"; }; rpi-netboot = nixosSystem' { extraArgs = { inherit nixpkgs; }; modules = [ nixos-hardware.nixosModules.raspberry-pi-4 self.nixosModules.rpi-netboot ./hosts/rpi-netboot ]; system = "aarch64-linux"; }; matemat = nixosSystem' { modules = [ ./config/lxc-container.nix ./hosts/containers/matemat yammat.nixosModule secrets.nixosModules.admins { nixpkgs.overlays = [ secrets.overlays.matemat ]; } ]; }; scrape = nixosSystem' { modules = [ ./config/lxc-container.nix ./hosts/containers/scrape { nixpkgs.overlays = [ secrets.overlays.scrape ]; } ]; extraArgs = { inherit scrapers; }; }; dn42 = nixosSystem' { modules = [ ./config/lxc-container.nix ./hosts/containers/dn42 { nixpkgs.overlays = [ secrets.overlays.dn42 ]; sops.defaultSopsFile = "${secrets}/hosts/dn42/secrets.yaml"; } ]; }; grafana = nixosSystem' { modules = [ ./config/lxc-container.nix ./hosts/containers/grafana ]; }; hydra = nixosSystem' { modules = [ ./config/lxc-container.nix ./hosts/containers/hydra { sops.defaultSopsFile = "${secrets}/hosts/hydra/secrets.yaml"; } ]; }; mucbot = nixosSystem' { modules = [ ./config/lxc-container.nix "${tigger}/module.nix" { nixpkgs.overlays = [ secrets.overlays.mucbot ]; } ./hosts/containers/mucbot ]; extraArgs = { inherit tigger; }; }; kibana = nixosSystem' { modules = [ ./config/lxc-container.nix ./hosts/containers/kibana ]; }; public-access-proxy = nixosSystem' { modules = [ ./config/lxc-container.nix ./hosts/containers/public-access-proxy ]; extraArgs = { inherit (self) nixosConfigurations; }; }; ticker = nixosSystem' { modules = [ ./config/lxc-container.nix "${ticker}/nixos-module.nix" ./hosts/containers/ticker ]; }; spaceapi = nixosSystem' { modules = [ ./config/lxc-container.nix "${spacemsg}/spaceapi/module.nix" ./hosts/containers/spaceapi ]; }; stream = nixosSystem' { modules = [ ./config/lxc-container.nix ./hosts/containers/stream ]; }; mobilizon = nixosSystem' { # TODO: pending https://github.com/NixOS/nixpkgs/pull/119132 nixpkgs = inputs.nixpkgs-mobilizon; modules = [ ./config/lxc-container.nix ./hosts/containers/mobilizon ]; }; mail = nixosSystem' { modules = [ ./config/lxc-container.nix ./hosts/containers/mail ]; }; logging = nixosSystem' { modules = [ ./config/lxc-container.nix ./hosts/containers/logging ]; }; keycloak = nixosSystem' { modules = [ ./config/lxc-container.nix ./hosts/containers/keycloak { nixpkgs.overlays = with secrets.overlays; [ keycloak ]; } ]; }; c3d2-web = nixosSystem' { nixpkgs = inputs.nixpkgs-unstable; modules = [ ./config/lxc-container.nix ./hosts/containers/c3d2-web { nixpkgs.overlays = [ secrets.overlays.c3d2-web ]; } ]; extraArgs = { inherit nixpkgs; }; }; sdrweb = nixosSystem' { nixpkgs = inputs.nixpkgs-openwebrx; modules = [ ./config/lxc-container.nix { nixpkgs.overlays = [ secrets.overlays.mucbot ]; } heliwatch.nixosModules.heliwatch ./hosts/containers/sdrweb ]; }; bind = nixosSystem' { modules = [ { nixpkgs.overlays = with secrets.overlays; [ bind ]; } ./config/lxc-container.nix ./hosts/containers/bind ]; }; jabber = nixosSystem' { modules = [ { nixpkgs.overlays = with secrets.overlays; [ jabber ]; } ./config/lxc-container.nix ./hosts/containers/jabber ]; }; storage-ng = nixosSystem' { modules = [ ./hosts/storage-ng secrets.nixosModules.admins { sops.defaultSopsFile = "${secrets}/hosts/storage-ng/secrets.yaml"; } ]; }; blogs = nixosSystem' { modules = [ self.nixosModules.plume ./config/lxc-container.nix ./hosts/containers/blogs { sops.defaultSopsFile = "${secrets}/hosts/blogs/secrets.yaml"; } ]; }; }; nixosModule = self.nixosModules.c3d2; nixosModules = { c3d2 = { imports = [ sops-nix.nixosModule ./modules/c3d2.nix ./modules/nncp.nix ./modules/autoupdate.nix ]; c3d2.hosts = hostRegistry.hosts; c3d2.users = import ./users.nix; c3d2.nncp.neigh = import ./config/nncp-relays.nix; }; nncp = ./modules/nncp.nix; plume = { imports = [ ./modules/plume.nix ]; nixpkgs.overlays = [ fenix.overlay naersk.overlay ]; }; rpi-netboot = ./modules/rpi-netboot.nix; }; hydraJobs = forAllSystems (system: builtins.mapAttrs (_: nixpkgs.lib.hydraJob) ( nixpkgs.lib.filterAttrs (_: toplevel: system == toplevel.system ) (builtins.mapAttrs (_: nixosSystem: nixosSystem.config.system.build.toplevel ) self.nixosConfigurations) ) ); }; }