{
  description = "Martin's NixOS configuration - Based on EmergentMind/nix-config";

  inputs = {
    #
    # ========= Official NixOS and HM Package Sources =========
    #
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
    # The next two are for pinning to stable vs unstable regardless of what the above is set to
    # This is particularly useful when an upcoming stable release is in beta because you can effectively
    # keep 'nixpkgs-stable' set to stable for critical packages while setting 'nixpkgs' to the beta branch to
    # get a jump start on deprecation changes.
    # See also 'stable-packages' and 'unstable-packages' overlays at 'overlays/default.nix"
    nixpkgs-stable.url = "github:NixOS/nixpkgs/nixos-25.05";
    nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";

    home-manager = {
      url = "github:nix-community/home-manager/release-25.05";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    #
    # ========= Utilities =========
    #
    # Secrets management
    sops-nix = {
      url = "github:mic92/sops-nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    # Catppuccin theming
    catppuccin.url = "github:catppuccin/nix";
    simple-nixos-mailserver.url = "gitlab:simple-nixos-mailserver/nixos-mailserver/nixos-25.05";
    # Spotify
    spicetify-nix = {
      url = "github:Gerg-L/spicetify-nix";
      inputs.nixpkgs.follows = "nixpkgs-unstable";
    };
    # Browser
    zen-browser.url = "github:0xc000022070/zen-browser-flake";
  };

  outputs =
    {
      self,
      nixpkgs,
      home-manager,
      ...
    }@inputs:
    let
      inherit (self) outputs;
      common = import ./shared/common.nix;
      theme = import ./shared/theme.nix;

      #
      # ========= Architectures =========
      #
      forAllSystems = nixpkgs.lib.genAttrs [
        "x86_64-linux"
        "aarch64-linux"
      ];

      # ========== Extend lib with lib.custom ==========
      # NOTE: This approach allows lib.custom to propagate into hm
      # see: https://github.com/nix-community/home-manager/pull/3454
      customLib = (_self: _super: { custom = import ./lib { inherit (nixpkgs) lib; }; });
      lib = nixpkgs.lib.extend customLib;
      libHm = home-manager.lib.extend customLib;

      systems = builtins.map (config: defaultAttrs // config) [
        {
          hostName = "desktop";
          nvidia.enable = true;
          ssh.publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMSzXyTuQyTrWsfORQbvgrqt/33+hfSUDXeMg6D1T2wz";
        }
        {
          hostName = "thinkpad";
          ssh.publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILNlHKE/BD8kKfhJD7GBk1A3whZf3gTjk9VEgGAj3qsH";
        }
        {
          hostName = "pi4";
          system = "aarch64-linux";
          wayland.enable = false;
          ssh.publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJE9m7YiITe1sDqSZ7Pa8luIw3WToLsypixZEqE4wCQE";
          address.private = common.localIpAddr 188;
        }
        {
          hostName = "homelab";
          wayland.enable = false;
          ssh.publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIARDv5nRlfPDXdV+Db4FaqeSJZ3/3MO0frYGzuVeqYAl";
          address.private = common.localIpAddr 231;
          address.tailnet = common.tailnetAddr "admin";
        }
        {
          hostName = "nidaros";
          wayland.enable = false;
          ssh.publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHVhsh6PQyLWy5C1VgyNJcV1zyqpTpD6lWPNWvSHsDD5";
          address.private = common.localIpAddr 228;
        }

      ];

      defaultAttrs = {
        hostName = builtins.abort "hostName is required";
        system = "x86_64-linux";
        username = common.username;
        version = common.system.version;
        wayland.enable = true;
        nvidia.enable = false;
      };

      knownSystems = [
        {
          # Samsung S23 FE
          hostName = "localhost-y4maoyqm";
          ssh.publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAII7SSjiqnjif1Kko60iXVTKJ7a1/lRlR8TFNtoclNcnQ";
        }
        {
          # OnePlus 8
          hostName = "localhost-4izgka9k";
          ssh.publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIALtulVgLrUEpKnpfPFQTHjaEXTxs2Q818NC18eLx0bj";
        }
      ];

    in
    {
      #
      # ========= Overlays =========
      #
      # Custom modifications/overrides to upstream packages
      overlays = import ./overlays.nix { inherit inputs; };

      #
      # ========= Host Configurations =========
      #
      nixosConfigurations = builtins.listToAttrs (
        builtins.map (
          {
            hostName,
            system,
            username,
            ...
          }@systemConfig:

          {
            name = hostName;
            value = nixpkgs.lib.nixosSystem {
              inherit system;
              specialArgs = {
                inherit
                  outputs
                  inputs
                  common
                  theme
                  lib
                  systemConfig
                  systems
                  knownSystems
                  ;
                isDarwin = false;
              };
              modules = [
                ./hosts/${hostName}
                home-manager.nixosModules.home-manager
                {
                  home-manager = {
                    # Backups conflicting files in case of error
                    backupFileExtension = "bkp";
                    useGlobalPkgs = true;
                    useUserPackages = true;
                    extraSpecialArgs = {
                      inherit
                        inputs
                        common
                        theme
                        libHm
                        systemConfig
                        systems
                        ;
                    };
                    users.${username} = import ./hosts/${hostName}/home-manager;
                  };
                }
                {
                  nixpkgs.overlays = [
                    # TODO temp fix for tailscale: https://github.com/tailscale/tailscale/issues/16966#issuecomment-3239543750
                    (_: prev: {
                      tailscale = prev.tailscale.overrideAttrs (old: {
                        checkFlags = builtins.map (
                          flag:
                          if prev.lib.hasPrefix "-skip=" flag then
                            flag + "|^TestGetList$|^TestIgnoreLocallyBoundPorts$|^TestPoller$"
                          else
                            flag
                        ) old.checkFlags;
                      });
                    })
                  ];
                }
              ];
            };
          }
        ) systems
      );

      #
      # ========= Formatting =========
      #
      # Nix formatter available through 'nix fmt' https://github.com/NixOS/nixfmt
      formatter = forAllSystems (system: nixpkgs.legacyPackages.${system}.nixfmt-rfc-style);

      #
      # ========= DevShell =========
      #
      # Custom shell for bootstrapping on new hosts, modifying nix-config, and secrets management
      devShells = forAllSystems (
        system:
        import ./shell.nix {
          pkgs = nixpkgs.legacyPackages.${system};
        }
      );
    };
}
