{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/release-24.05";

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

    crane = {
      url = "github:ipetkov/crane";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs =
    {
      self,
      nixpkgs,
      flake-utils,
      crane,
      ...
    }:
    flake-utils.lib.eachDefaultSystem (
      system:
      let
        pkgs = import nixpkgs { inherit system; };
        inherit (pkgs) lib;

        craneLib = crane.mkLib pkgs;

        commonArgs = {
          src = craneLib.cleanCargoSource ./.;
          strictDeps = true;
          nativeBuildInputs = with pkgs; [ pkg-config ];
          buildInputs = with pkgs; [ openssl ];
        };

        cargoArtifacts = craneLib.buildDepsOnly commonArgs;

        my-crate = craneLib.buildPackage (
          commonArgs
          // {
            src = ./.; # Allow access to assets, like ./templates
            inherit cargoArtifacts;
            meta.mainProgram = "warthunder-leak-counter";
          }
        );
      in
      {
        checks = {
          my-crate-fmt = craneLib.cargoFmt { inherit (commonArgs) src; };
        };

        packages.default = my-crate;

        nixosModules.default =
          { pkgs, config, ... }:
          let
            cfg = config.services.warthunder-leak-counter;
          in
          {
            options.services.warthunder-leak-counter = {
              enable = lib.mkEnableOption "Enable Warthunder Leak Counter";

              staticDir = lib.mkOption {
                default = toString ./static;
                type = lib.types.str;
              };
              port = lib.mkOption {
                type = lib.types.port;
                default = 6263;
              };
            };
            config = lib.mkIf cfg.enable {
              systemd.services.warthunder-leak-counter = {
                script = lib.getExe my-crate;

                environment = {
                  WARTHUNDER_LEAK_SERVE_PORT = toString cfg.port;
                  WARTHUNDER_LEAK_STATIC_DIR = cfg.staticDir;
                };

                after = [ "network.target" ];
                wantedBy = [ "network.target" ];
              };
            };
          };

        packages.nixosConfigurations.test-server = nixpkgs.lib.nixosSystem {
          inherit system;
          modules = [
            self.nixosModules.${system}.default
            (nixpkgs + "/nixos/modules/virtualisation/qemu-vm.nix")
            (
              { config, ... }:
              {
                services.warthunder-leak-counter.enable = true;
                users.users.root.password = "root";
                networking.firewall.enable = false;
                virtualisation.forwardPorts = [
                  {
                    from = "host";
                    host.port = 8888;
                    guest.port = config.services.warthunder-leak-counter.port;
                  }
                ];
                system.stateVersion = "24.05";
              }
            )
          ];
        };

        devShells.default = pkgs.mkShell {
          inherit (commonArgs) buildInputs;

          nativeBuildInputs =
            with pkgs;
            (
              [
                rustc
                cargo
                rustfmt
                rust-analyzer
                clippy
                cargo-feature
                cargo-watch
                curl
              ]
              ++ commonArgs.nativeBuildInputs
            );
        };
      }
    );
}
