{
  description = "Nomarchy - A NixOS-based distribution with Omarchy flavour";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-25.11";
    nixos-hardware.url = "github:NixOS/nixos-hardware";
    
    disko = {
      url = "github:nix-community/disko";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    impermanence = {
      url = "github:nix-community/impermanence";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    
    home-manager = {
      url = "github:nix-community/home-manager/release-25.11";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    
    nix-colors = {
      url = "github:misterio77/nix-colors";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    stylix = {
      url = "github:danth/stylix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    walker = {
      url = "github:abenz1267/walker";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, nixos-hardware, disko, impermanence, home-manager, nix-colors, stylix, walker, ... } @ inputs: let
    system = "x86_64-linux";
    lib = nixpkgs.lib;

    # Overlays
    overlays = [
      (final: prev: {
        makima-bin = prev.makima;
        nomarchy-system-scripts = final.callPackage ./core/system/scripts-derivation.nix { };
      })
    ];

    pkgs = import nixpkgs {
      inherit system overlays;
      config.allowUnfree = true;
    };

    # Helper to create standalone home configurations
    mkHome = { username, modules ? [] }: home-manager.lib.homeManagerConfiguration {
      inherit pkgs;
      extraSpecialArgs = { inherit inputs; };
      modules = [
        ./features
        {
          home.username = username;
          home.homeDirectory = "/home/${username}";
          home.stateVersion = "25.11";
        }
      ] ++ modules;
    };

    homeConfigs = {
      "nixos" = mkHome { username = "nixos"; };
      "nomarchy" = mkHome { username = "nomarchy"; };
    };

    # Realise the full HM generation for every palette so theme switches hit
    # the Nix cache instead of rebuilding Stylix outputs per swap. Each entry
    # pins `nomarchy.theme` (and clears the wallpaper override so the theme's
    # own default background is used) and exposes the generation's activation
    # package under its theme name.
    palettes = import ./themes/palettes;
    themeNames = builtins.attrNames palettes;
    themeVariantFor = themeName: (mkHome {
      username = "nomarchy";
      modules = [{
        nomarchy.theme = lib.mkForce themeName;
        nomarchy.wallpaper = lib.mkForce "";
      }];
    }).activationPackage;
    allThemeVariants = pkgs.linkFarm "nomarchy-all-theme-variants"
      (map (name: { inherit name; path = themeVariantFor name; }) themeNames);
  in {
    # Expose inputs for downstream use
    inherit inputs;

    nixosModules = {
      system = import ./core;
      home = import ./features;
    };

    nixosConfigurations = {
      # Minimal TTY installer ISO (new golden path)
      installerIso = nixpkgs.lib.nixosSystem {
        specialArgs = { inherit inputs; };
        modules = [
          { 
            nixpkgs.hostPlatform = "x86_64-linux";
            nixpkgs.overlays = overlays;
          }
          "${nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix"
          ./hosts/installer-iso.nix
          {
            system.stateVersion = "25.11";
            networking.hostName = "nomarchy-installer";
          }
        ];
      };

      # Graphical installer ISO (legacy, for users who prefer GUI)
      installerIsoGraphical = nixpkgs.lib.nixosSystem {
        specialArgs = { inherit inputs; };
        modules = [
          {
            nixpkgs.hostPlatform = "x86_64-linux";
            nixpkgs.overlays = overlays;
          }
          "${nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-graphical-base.nix"
          home-manager.nixosModules.home-manager
          ./hosts/live-iso.nix
          ./core
          {
            system.stateVersion = "25.11";
            services.displayManager.autoLogin.enable = true;
            services.displayManager.autoLogin.user = "nixos";

            home-manager.useGlobalPkgs = true;
            home-manager.useUserPackages = true;
            home-manager.backupFileExtension = "hm-bak";
            home-manager.extraSpecialArgs = { inherit inputs; };
            home-manager.users.nixos = {
              imports = [ ./features ];
              home.stateVersion = "25.11";
            };
          }
        ];
      };

      # Default configuration (VM and testing)
      default = nixpkgs.lib.nixosSystem {
        specialArgs = { inherit inputs; };
        modules = [
          {
            nixpkgs.hostPlatform = "x86_64-linux";
            nixpkgs.overlays = overlays;
          }
          home-manager.nixosModules.home-manager
          ./core/default.nix
          ./core/system/vm-guest.nix
          {
            system.stateVersion = "25.11";
            networking.hostName = "nomarchy";

            virtualisation.vmVariant.virtualisation.memorySize = 4096;
            virtualisation.vmVariant.virtualisation.cores = 2;

            # Setup default user for testing
            users.users.nomarchy = {
              isNormalUser = true;
              extraGroups = [ "wheel" "video" "render" "networkmanager" ];
              initialPassword = "nixos";
            };

            services.displayManager.autoLogin.enable = true;
            services.displayManager.autoLogin.user = "nomarchy";

            home-manager.useGlobalPkgs = true;
            home-manager.useUserPackages = true;
            home-manager.backupFileExtension = "hm-bak";
            home-manager.extraSpecialArgs = { inherit inputs; };
            home-manager.users.nomarchy = {
              imports = [ ./features ];
              home.stateVersion = "25.11";
            };
          }
        ];
      };
    };

    homeConfigurations = homeConfigs;

    packages.${system} = {
      # Pre-cache every theme's Home Manager generation so subsequent
      # `nomarchy-theme-set` swaps are cache hits (no Stylix rebuild, no
      # downloads). Run once after install:
      #     nix build /etc/nomarchy#allThemeVariants --no-link
      inherit allThemeVariants;
      default = allThemeVariants;
    };
  };
}
