{ system ? "x86_64-linux"
, inputs ? import ./inputs.nix { inherit system; }
}:

let
  inherit (inputs) sops-nix home-manager nixpkgs;

  mkConfig = imports: config:
    inputs.nixpkgs-flake.lib.nixosSystem rec {
      system = "x86_64-linux";
      modules = [ config ] ++ imports;
      extraModules =
        builtins.attrValues self.modules
        ++ [ sops-nix ];
      pkgs = nixpkgs;

      specialArgs = {
        inherit inputs system;
        craneLib = inputs.crane.lib.${system};
      };
    };
  mkDesktop = mkConfig [ ./common/desktop.nix ];
  mkServer = mkConfig [
    ./common/headless.nix
  ];
  deploy-vps = hostname: {
    inherit hostname;
    profiles.system = {
      user = "root";
      sshUser = "root";
      path = inputs.deploy-rs.lib.x86_64-linux.activate.nixos
        self.nixosConfigurations.${hostname};
    };
  };

  self = {

    nixosConfigurations = {
      chaski = mkServer ./chaski/configuration.nix;
      flora = mkServer ./flora/configuration.nix;
      abbenay = mkDesktop ./abbenay/configuration.nix;
      cyberbox = mkDesktop ./cyberbox/configuration.nix;
      ilex = mkDesktop ./ilex/configuration.nix;
    };

    deploy.nodes = {
      chaski = deploy-vps "chaski";
      flora = deploy-vps "flora";
      parsons = {
        hostname = "parsons";
        profiles.home = {
          user = "stuebinm";
          sshUser = "stuebinm";
          path = inputs.deploy-rs.lib.x86_64-linux.activate.home-manager
            self.homeConfigurations.stuebinm-minimal;
        };
      };
      abbenay = {
        hostname = "abbenay";
        profiles.system = {
          user = "root";
          sshUser = "stuebinm";
          interactiveSudo = true;
          path = inputs.deploy-rs.lib.x86_64-linux.activate.nixos
            self.nixosConfigurations.abbenay;
        };
      };
    };

    homeConfigurations = let
      home = root:
        home-manager.lib.homeManagerConfiguration rec {
          pkgs = nixpkgs;
          modules = [ root ];
          extraSpecialArgs = {
            inherit inputs;
            system = "x86_64-linux";
          };
        };
    in {
      stuebinm = home ./home/home.nix;
      stuebinm-minimal = home ./home/home-minimal.nix;
    };

    home = self.homeConfigurations.stuebinm.activationPackage;
    home-minimal = self.homeConfigurations.stuebinm-minimal.activationPackage;

    modules = import ./modules;
    packages = import ./pkgs { inherit nixpkgs; };
    tests = import ./tests { inherit nixpkgs; };
  };

in self