{
  description = "My awesome system config";

  inputs = {
    # Nix
    agenix.inputs.nixpkgs.follows = "nixpkgs";
    agenix.url = "github:ryantm/agenix";

    devshell.url = "github:numtide/devshell";

    disko.url = "github:nix-community/disko";

    flake-parts.url = "github:hercules-ci/flake-parts";
    flake-utils.url = "github:numtide/flake-utils";

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

    impermanence.url = "github:nix-community/impermanence";

    master.url = "github:nixos/nixpkgs";
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    stable.url = "github:nixos/nixpkgs/nixos-24.11";

    pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";

    nur.url = "github:nix-community/NUR";

    nix-index-database.url = "github:Mic92/nix-index-database";
    nix-index-database.inputs.nixpkgs.follows = "nixpkgs";

    nixos-facter-modules.url = "github:numtide/nixos-facter-modules";

    nix-monitored.url = "github:ners/nix-monitored";
    nix-monitored.inputs.nixpkgs.follows = "nixpkgs";

    jovian.url = "github:Jovian-Experiments/Jovian-NixOS";

    search.url = "github:NuschtOS/search";
    search.inputs.nixpkgs.follows = "nixpkgs";

    # Programs
    timers.url = "git+https://gitea.moritzboeh.me/moritz/timers.git";
    filetags.url = "git+https://gitea.moritzboeh.me/moritz/filetags.git";

    # Neovim
    neovim-nightly-overlay.url = "github:nix-community/neovim-nightly-overlay";

    nixvim.url = "github:nix-community/nixvim";
    nixvim.inputs.devshell.follows = "devshell";
    nixvim.inputs.flake-parts.follows = "flake-parts";
    nixvim.inputs.home-manager.follows = "home-manager";

    # Niri
    niri.url = "github:sodiboo/niri-flake";
    niri.inputs.nixpkgs.follows = "nixpkgs";

    # Laptop
    asus-touchpad-numpad-driver.url = "github:MoritzBoehme/asus-touchpad-numpad-driver/german-layout";
    asus-touchpad-numpad-driver.flake = false;

    # Firefox user.js
    arkenfox-userjs.url = "github:arkenfox/user.js";
    arkenfox-userjs.flake = false;
  };

  outputs = inputs@{ self, flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      debug = true; # NOTE: for nixd
      imports = [
        inputs.pre-commit-hooks.flakeModule
        inputs.devshell.flakeModule
      ];

      systems = [ "x86_64-linux" ];
      perSystem = { config, self', inputs', pkgs, system, ... }: {
        devshells.default = {
          devshell.startup.pre-commit-hook.text = config.pre-commit.installationScript;
          commands = [
            {
              name = "agenix";
              help = "wrapper around agenix";
              command = ''
                sudo EDITOR="${pkgs.lib.getExe pkgs.vim}" ${pkgs.lib.getExe' inputs'.agenix.packages.default "agenix"} --identity /etc/ssh/ssh_host_ed25519_key "$@"
              '';
            }
            {
              name = "vim-startuptime";
              help = "test vim startup time";
              command = "${pkgs.lib.getExe pkgs.hyperfine} --warmup 5 --prepare 'sleep 2' 'vim -c exit'";
            }
          ];
          packages = with pkgs; [
            stylua
            sumneko-lua-language-server
            nixpkgs-fmt
            nh
          ];
        };

        pre-commit = {
          check.enable = true;
          settings = {
            hooks = {
              nixpkgs-fmt.enable = true;
              statix.enable = true;
              shellcheck.enable = true;
              stylua.enable = true;
            };
          };
        };

        legacyPackages = pkgs;
        packages =
          let
            modules = [
              { name = "My stuff"; }
            ];
            mkName = name: "${name} Search";
            mkSearch = name: value: inputs'.search.packages.mkSearch {
              urlPrefix = "file://${self}/flake.nix";
              title = mkName name;
              optionsJSON = "${(pkgs.nixosOptionsDoc { inherit (value) options; warningsAreErrors = false; }).optionsJSON}/share/doc/nixos/options.json";
            };
            serveSearch = name: value:
              pkgs.writeShellApplication {
                name = mkName name;
                text = ''
                  xdg-open http://127.0.0.1:8080
                  static-web-server -p 8080 -d ${mkSearch name value}
                '';
                runtimeInputs = with pkgs; [ static-web-server xdg-utils ];
              };
          in
          pkgs.lib.mapAttrs serveSearch self.nixosConfigurations;
      };

      flake = {
        lib = inputs.nixpkgs.lib.extend
          (self: _: { my = import ./lib { lib = self; }; });

        nixosConfigurations = self.lib.my.mapModules
          (path: self.lib.nixosSystem {
            inherit (self) lib;
            specialArgs = {
              inherit inputs self;
            };
            modules =
              [
                ./modules
                {
                  home-manager = {
                    useGlobalPkgs = true;
                    useUserPackages = true;
                    extraSpecialArgs = { inherit inputs self; };
                    sharedModules = [
                      inputs.nixvim.homeManagerModules.nixvim
                    ];
                  };
                }
                inputs.agenix.nixosModules.age
                inputs.disko.nixosModules.default
                inputs.home-manager.nixosModules.default
                inputs.impermanence.nixosModules.impermanence
                inputs.nix-index-database.nixosModules.nix-index
                inputs.nixos-facter-modules.nixosModules.facter
                inputs.niri.nixosModules.niri
                inputs.jovian.nixosModules.default
                path
              ];
          })
          ./hosts;

        templates = {
          python = {
            description = "Simple poetry shell.nix";
            path = ./templates/python;
          };
          rust = {
            description = "Crane + Fenix flake with mold for faster local builds.";
            path = ./templates/rust;
          };
        };
      };
    };

  nixConfig = {
    extra-substituters = [
      "https://nix-community.cachix.org?priority=2"
      "https://cache.lix.systems?priority=3"
      "https://pre-commit-hooks.cachix.org?priority=4"
    ];
    extra-trusted-public-keys = [
      "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
      "cache.lix.systems:aBnZUw8zA7H35Cz2RyKFVs3H4PlGTLawyY5KRbvJR8o="
      "pre-commit-hooks.cachix.org-1:Pkk3Panw5AW24TOv6kz3PvLhlH8puAsJTBbOPmBo7Rc="
    ];
  };
}
