repositories

NixDaemon

NixOS pentest workstation as one flake — IceBreaker's successor

clonegit clone https://git.daemon-sec.xyz/NixDaemon.git
default.nixNix269 lines9.9 KiBraw download
# modules/hosts/generic/default.nix — NixDaemon for any machine: a PC, a
# laptop, or a VM, x86_64 or aarch64.
#
#   sudo nixos-install --flake .#nixdaemon           (from the installer)
#   sudo nixos-rebuild switch --flake .#nixdaemon    (afterwards)
#   nh os switch -H nixdaemon                        (the same, with a diff)
#
# Everything you change lives in two files beside this one:
#   _settings.nix               user, hostname, arch, boot mode, VM, desktop, toolkit
#   _hardware-configuration.nix your disks, from nixos-generate-config
#
# This host is deliberately separate from modules/hosts/laptop (the author's
# machine). It shares the toolkit (modules/features/pentest), the Niri desktop
# and the self-contained home modules, and none of the personal parts: no
# sops secrets, no private dotfiles checkout, no GPG/SSH identity, no fan or
# NVIDIA workarounds. It builds without anyone's keys.
#
# More than one machine? `self.lib.mkHost` takes a settings set, so a second
# host is one line:
#   flake.nixosConfigurations.vm = self.lib.mkHost (import ./_settings.nix // { hostName = "vm"; vm = "qemu"; });
# (it also needs its own hardware configuration — see mkHost's `hardware`.)
{ self, inputs, ... }:
{
  flake.lib.mkHost =
    settings:
    inputs.nixpkgs.lib.nixosSystem {
      specialArgs = {
        inherit inputs settings;
        inherit (settings) user;
      };
      modules = [
        (settings.hardware or ./_hardware-configuration.nix)
        self.nixosModules.generic
      ];
    };

  flake.nixosConfigurations.nixdaemon = self.lib.mkHost (import ./_settings.nix);

  flake.nixosModules.generic =
    {
      config,
      pkgs,
      lib,
      user,
      settings,
      ...
    }:
    let
      s = settings;
      niri = s.desktop == "niri";
      xfce = s.desktop == "xfce";
      i3 = s.desktop == "i3";
      gui = s.desktop != "none";
    in
    {
      imports = [
        inputs.home-manager.nixosModules.home-manager
        self.nixosModules.pentest
      ]
      # desktop-options asserts that Hyprland or Niri is on, so it is only
      # pulled in when Niri is the desktop.
      ++ lib.optionals niri [
        self.nixosModules.desktop-options
        self.nixosModules.desktop-niri
      ]
      # The X11 desktops for VMs without 3D (modules/features/desktop/).
      ++ lib.optional xfce self.nixosModules.desktop-xfce
      ++ lib.optional i3 self.nixosModules.desktop-i3;

      config = lib.mkMerge [
        {
          assertions = [
            {
              assertion = builtins.elem s.desktop [
                "niri"
                "xfce"
                "i3"
                "none"
              ];
              message = "_settings.nix: desktop must be \"niri\", \"xfce\", \"i3\" or \"none\" (got \"${s.desktop}\")";
            }
            {
              assertion = builtins.elem s.vm [
                "none"
                "vmware"
                "virtualbox"
                "qemu"
                "hyperv"
              ];
              message = "_settings.nix: vm must be none, vmware, virtualbox, qemu or hyperv (got \"${s.vm}\")";
            }
            {
              assertion = builtins.elem s.boot [
                "efi"
                "bios"
              ];
              message = "_settings.nix: boot must be \"efi\" or \"bios\" (got \"${s.boot}\")";
            }
            {
              # apktool pulls in aapt, which Google only ships for x86_64.
              assertion = !(s.system == "aarch64-linux" && (s.pentest.mobile or false));
              message = "_settings.nix: pentest.mobile is x86_64-only (aapt); turn it off on aarch64";
            }
          ];

          nixpkgs.hostPlatform = lib.mkDefault s.system;
          nixpkgs.config.allowUnfree = true; # burpsuite, and the NVIDIA/VMware bits some machines need

          ##### Boot ###############################################################
          boot.loader =
            if s.boot == "efi" then
              {
                systemd-boot.enable = true;
                systemd-boot.configurationLimit = 20;
                efi.canTouchEfiVariables = true;
              }
            else
              {
                grub.enable = true;
                grub.device = s.biosDevice;
                grub.configurationLimit = 20;
              };

          ##### Machine ############################################################
          networking.hostName = s.hostName;
          networking.networkmanager.enable = true;
          time.timeZone = s.timeZone;
          i18n.defaultLocale = s.locale;
          services.xserver.xkb.layout = s.keyboard;
          console.useXkbConfig = true;

          users.users.${user} = {
            isNormalUser = true;
            extraGroups = [
              "networkmanager"
              "wheel"
            ]
            # VirtualBox shared folders appear under /media/sf_<name> for this group.
            ++ lib.optional (s.vm == "virtualbox" && (s.sharedFolders or false)) "vboxsf";
            shell = pkgs.zsh;
            initialPassword = s.initialPassword;
            openssh.authorizedKeys.keys = s.sshKeys;
          };

          programs.zsh.enable = true;
          programs.nix-ld.enable = true; # prebuilt binaries (uv pythons, npm, Go releases)

          services.openssh = lib.mkIf s.ssh {
            enable = true;
            settings.PasswordAuthentication = false;
            settings.PermitRootLogin = "no";
          };

          ##### Nix ################################################################
          nix.settings.experimental-features = [
            "nix-command"
            "flakes"
          ];
          programs.nh = {
            enable = true;
            flake = "/home/${user}/NixDaemon"; # where docs/install.md clones it
            clean = {
              enable = true;
              dates = "weekly";
              extraArgs = "--keep 5 --keep-since 14d";
            };
          };
          environment.systemPackages = with pkgs; [
            git
            nvd
            nix-output-monitor
            pciutils
            usbutils
          ];

          ##### Hypervisor guest tools #############################################
          virtualisation.vmware.guest.enable = s.vm == "vmware";
          virtualisation.virtualbox.guest.enable = s.vm == "virtualbox";
          virtualisation.hypervGuest.enable = s.vm == "hyperv";
          services.qemuGuest.enable = s.vm == "qemu";
          services.spice-vdagentd.enable = s.vm == "qemu"; # clipboard + resize in virt-manager/UTM

          # VMware: open-vm-tools above gives time sync, clean shutdown, and —
          # under X11 (xfce, i3) — clipboard and automatic resize. The SVGA
          # adapter's 3D needs Mesa's vmwgfx driver, hence graphics on.
          hardware.graphics.enable = lib.mkIf (s.vm == "vmware" && gui) true;
          # Host folders shared in VMware's settings, at /mnt/hgfs/<name>.
          fileSystems."/mnt/hgfs" = lib.mkIf (s.vm == "vmware" && (s.sharedFolders or false)) {
            device = ".host:/";
            fsType = "fuse./run/current-system/sw/bin/vmhgfs-fuse";
            options = [ "umask=22" "uid=1000" "gid=100" "allow_other" "auto_unmount" "defaults" "nofail" "x-systemd.automount" ];
          };

          ##### Desktop ############################################################

          services.greetd = lib.mkIf niri {
            enable = true;
            useTextGreeter = true;
            settings.default_session.command = lib.concatStringsSep " " [
              "${pkgs.tuigreet}/bin/tuigreet"
              "--time"
              "--remember"
              "--remember-session"
              "--asterisks"
              "--sessions ${config.services.displayManager.sessionData.desktops}/share/wayland-sessions"
            ];
          };


          security.polkit.enable = lib.mkIf gui true;
          services.udisks2.enable = lib.mkIf gui true;
          services.gnome.gnome-keyring.enable = lib.mkIf gui true;
          programs.dconf.enable = lib.mkIf gui true;
          services.pulseaudio.enable = false;
          security.rtkit.enable = lib.mkIf gui true;
          services.pipewire = lib.mkIf gui {
            enable = true;
            alsa.enable = true;
            pulse.enable = true;
          };

          environment.sessionVariables = lib.mkIf niri {
            ELECTRON_OZONE_PLATFORM_HINT = "auto";
            NIXOS_OZONE_WL = "1";
          };

          fonts.packages = with pkgs; [
            nerd-fonts.jetbrains-mono # the terminal font (kitty, below)
            noto-fonts
            noto-fonts-color-emoji
          ];

          ##### Toolkit ############################################################
          daemon.pentest = {
            enable = true;
            # BloodHound CE's pinned neo4j is published for x86_64 only.
            bloodhound.enable = lib.mkDefault (s.system == "x86_64-linux");
          }
          // lib.mapAttrs (_: on: { enable = on; }) s.pentest;

          ##### Home ###############################################################
          home-manager = {
            useGlobalPkgs = true;
            useUserPackages = true;
            backupFileExtension = "hm-bak";
            extraSpecialArgs = { inherit inputs user; };
            users.${user} = self.homeModules.generic;
          };

          system.stateVersion = "26.05";
        }

        # optionalAttrs, not mkIf: daemon.desktop is only declared when Niri
        # imports desktop-options above, and mkIf still needs the option to exist.
        (lib.optionalAttrs niri {
          daemon.desktop = {
            hyprland.enable = false; # Hyprland's home side needs the author's dotfiles
            niri.enable = true;
              # The neutral desktop (no author location/wallpaper/font), with
              # this machine's keyboard layout.
              niri.package = self.legacyPackages.${s.system}.mkNiri {
                noctaliaPkg = self.packages.${s.system}.noctalia;
                xkb.layout = s.keyboard;
              };
          };
        })
      ];
    };
}