{
  description = "A very basic flake";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-25.11";
    nixpkgs-unstable.url = "github:nixos/nixpkgs/nixos-unstable";
    nixos-wsl.url = "github:nix-community/NixOS-WSL/main";
    
    nix-darwin = {
      url = "github:nix-darwin/nix-darwin/nix-darwin-25.11";
      inputs.nixpkgs.follows = "nixpkgs";
    };

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

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

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

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

    google-cookie-retrieval = {
      url = "git+https://git.johnogle.info/johno/google-cookie-retrieval.git";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    jovian = {
      url = "github:Jovian-Experiments/Jovian-NixOS";
      inputs.nixpkgs.follows = "nixpkgs-unstable";
    };

    beads = {
      url = "github:steveyegge/beads";
      inputs.nixpkgs.follows = "nixpkgs-unstable";
    };
  };

  outputs = { self, nixpkgs, nixpkgs-unstable, nixos-wsl, ... } @ inputs: let
    nixosModules = [
      ./roles
    ] ++ [
      inputs.home-manager.nixosModules.home-manager
      {
        nixpkgs.overlays = [
          (final: prev: {
            unstable = import nixpkgs-unstable {
              system = prev.stdenv.hostPlatform.system;
              config.allowUnfree = true;
            };
            custom = prev.callPackage ./packages {};
            # Compatibility: bitwarden renamed to bitwarden-desktop in unstable
            bitwarden-desktop = prev.bitwarden-desktop or prev.bitwarden;
          })
        ];
        home-manager.useGlobalPkgs = true;
        home-manager.useUserPackages = true;
        home-manager.sharedModules = [
          inputs.plasma-manager.homeModules.plasma-manager
        ];
        home-manager.extraSpecialArgs = {
          globalInputs = inputs;
        };
      }
    ];
    # Modules for unstable-based systems (like nix-deck)
    nixosModulesUnstable = [
      ./roles
    ] ++ [
      inputs.home-manager-unstable.nixosModules.home-manager
      inputs.jovian.nixosModules.jovian
      {
        nixpkgs.overlays = [
          (final: prev: {
            unstable = import nixpkgs-unstable {
              system = prev.stdenv.hostPlatform.system;
              config.allowUnfree = true;
            };
            custom = prev.callPackage ./packages {};
            # Compatibility: bitwarden renamed to bitwarden-desktop in unstable
            bitwarden-desktop = prev.bitwarden-desktop or prev.bitwarden;
          })
        ];
        home-manager.useGlobalPkgs = true;
        home-manager.useUserPackages = true;
        home-manager.sharedModules = [
          inputs.plasma-manager-unstable.homeModules.plasma-manager
        ];
        home-manager.extraSpecialArgs = {
          globalInputs = inputs;
        };
      }
    ];
    darwinModules = [
      ./roles/darwin.nix
    ] ++ [
      inputs.home-manager.darwinModules.home-manager
      {
        nixpkgs.overlays = [
          (final: prev: {
            unstable = import nixpkgs-unstable {
              system = prev.stdenv.hostPlatform.system;
              config.allowUnfree = true;
              overlays = [
                # Override claude-code in unstable to use our custom GCS-based build
                # (needed for corporate networks that block npm registry)
                (ufinal: uprev: {
                  claude-code = prev.custom.claude-code or (prev.callPackage ./packages {}).claude-code;
                })
              ];
            };
            custom = prev.callPackage ./packages {};
            # Compatibility: bitwarden renamed to bitwarden-desktop in unstable
            bitwarden-desktop = prev.bitwarden-desktop or prev.bitwarden;
          })
        ];
        home-manager.useGlobalPkgs = true;
        home-manager.useUserPackages = true;
        home-manager.extraSpecialArgs = {
          globalInputs = inputs;
        };
      }
    ];

  in {
    nixosConfigurations.nix-book = nixpkgs.lib.nixosSystem rec {
      system = "x86_64-linux";
      modules = nixosModules ++ [
        ./machines/nix-book/configuration.nix
        {
          home-manager.users.johno = {
            imports = [ ./home/home-laptop-compact.nix ];
            # Machine-specific overrides
            home.roles.i3_sway.extraSwayConfig = {
              output.eDP-1.scale = "1.75";
            };
          };
          home-manager.extraSpecialArgs = { inherit system; };
        }
      ];
    };

    nixosConfigurations.boxy = nixpkgs.lib.nixosSystem rec {
      system = "x86_64-linux";
      modules = nixosModules ++ [
        ./machines/boxy/configuration.nix
        {
          home-manager.users.johno = import ./home/home-media-center.nix;
          home-manager.users.kodi = import ./home/home-kodi.nix;
          home-manager.extraSpecialArgs = { inherit system; };
        }
      ];
    };

    nixosConfigurations.wixos = nixpkgs.lib.nixosSystem rec {
      system = "x86_64-linux";
      modules = nixosModules ++ [
        nixos-wsl.nixosModules.default
        ./machines/wixos/configuration.nix
        inputs.home-manager.nixosModules.home-manager
        {
          home-manager.users.johno = import ./home/home-desktop.nix;
          home-manager.extraSpecialArgs = { inherit system; };
        }
      ];
    };

    nixosConfigurations.zix790prors = nixpkgs.lib.nixosSystem rec {
      system = "x86_64-linux";
      modules = nixosModules ++ [
        ./machines/zix790prors/configuration.nix
        inputs.home-manager.nixosModules.home-manager
        {
          home-manager.users.johno = import ./home/home-desktop.nix;
          home-manager.extraSpecialArgs = { inherit system; };
        }
      ];
    };

    # Live USB ISO configuration
    nixosConfigurations.live-usb = nixpkgs.lib.nixosSystem rec {
      system = "x86_64-linux";
      modules = nixosModules ++ [
        ./machines/live-usb/configuration.nix
        {
          home-manager.users.nixos = import ./home/home-live-usb.nix;
          home-manager.extraSpecialArgs = { inherit system; };
        }
      ];
    };

    # Steam Deck configuration (using unstable for better Jovian compatibility)
    nixosConfigurations.nix-deck = nixpkgs-unstable.lib.nixosSystem rec {
      system = "x86_64-linux";
      modules = nixosModulesUnstable ++ [
        ./machines/nix-deck/configuration.nix
        {
          home-manager.users.johno = import ./home/home-desktop.nix;
          home-manager.extraSpecialArgs = { inherit system; };
        }
      ];
    };

    # ZFS/NFS server configuration
    nixosConfigurations.john-endesktop = nixpkgs.lib.nixosSystem rec {
      system = "x86_64-linux";
      modules = nixosModules ++ [
        ./machines/john-endesktop/configuration.nix
        # Minimal server - no home-manager needed
      ];
    };

    # Darwin/macOS configurations
    darwinConfigurations."blkfv4yf49kt7" = inputs.nix-darwin.lib.darwinSystem rec {
      system = "aarch64-darwin";
      modules = darwinModules ++ [
        ./machines/johno-macbookpro/configuration.nix
        {
          home-manager.users.johno = import ./home/home-darwin-work.nix;
          home-manager.extraSpecialArgs = { inherit system; };
        }
      ];
    };

    # Flake apps
    apps = nixpkgs.lib.genAttrs [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" ] (system:
      let
        pkgs = import nixpkgs { inherit system; };
        commonDeps = [ pkgs.curl pkgs.jq pkgs.nix pkgs.git pkgs.gnused pkgs.gnugrep pkgs.coreutils pkgs.gawk ];

        update-doomemacs = pkgs.writeShellScriptBin "update-doomemacs" ''
          export PATH="${pkgs.lib.makeBinPath commonDeps}:$PATH"
          ${builtins.readFile ./scripts/update-doomemacs.sh}
        '';

        update-claude-code = pkgs.writeShellScriptBin "update-claude-code" ''
          export PATH="${pkgs.lib.makeBinPath commonDeps}:$PATH"
          ${builtins.readFile ./packages/claude-code/update.sh}
        '';

        rotate-wallpaper = pkgs.writeShellScriptBin "rotate-wallpaper" ''
          export PATH="${pkgs.lib.makeBinPath commonDeps}:$PATH"
          ${builtins.readFile ./scripts/rotate-wallpaper.sh}
        '';

        upgrade = pkgs.writeShellScriptBin "upgrade" ''
          export PATH="${pkgs.lib.makeBinPath commonDeps}:$PATH"
          ${builtins.readFile ./scripts/upgrade.sh}
        '';
      in {
        update-doomemacs = {
          type = "app";
          program = "${update-doomemacs}/bin/update-doomemacs";
        };
        update-claude-code = {
          type = "app";
          program = "${update-claude-code}/bin/update-claude-code";
        };
        rotate-wallpaper = {
          type = "app";
          program = "${rotate-wallpaper}/bin/rotate-wallpaper";
        };
        upgrade = {
          type = "app";
          program = "${upgrade}/bin/upgrade";
        };
      }
    );
  };
}
