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

    rust-overlay.url = "github:oxalica/rust-overlay";
    rust-overlay.inputs.nixpkgs.follows = "nixpkgs";

    basecamp.url = "github:plul/basecamp";
    basecamp.inputs.nixpkgs.follows = "nixpkgs";
    basecamp.inputs.rust-overlay.follows = "rust-overlay";
  };

  outputs =
    {
      self,
      basecamp,
      nixpkgs,
      ...
    }:
    {
      basecamp-config = {
        rust.enable = true;
        rust.toolchain.targets = [ "wasm32-unknown-unknown" ];
      };

      packages."x86_64-linux".default =
        let
          cargoToml = builtins.fromTOML (builtins.readFile ./crates/ascend/Cargo.toml);
          pkgs = import nixpkgs { system = "x86_64-linux"; };
          config =
            (basecamp.eval.components {
              inherit pkgs;
              config = self.basecamp-config;
            }).config;
          rust-toolchain = config.rust.toolchain.package;
          rustPlatform = pkgs.makeRustPlatform {
            cargo = rust-toolchain;
            rustc = rust-toolchain;
          };
        in
        rustPlatform.buildRustPackage {
          pname = cargoToml.package.name;
          version = cargoToml.package.version;
          src = ./.;
          cargoLock.lockFile = ./Cargo.lock;

          nativeBuildInputs = [
            pkgs.cargo-leptos
            pkgs.dart-sass
            pkgs.tailwindcss_4

            # For optimizing wasm release builds
            pkgs.binaryen

            pkgs.makeWrapper
          ];

          buildPhase = ''
            cargo leptos build --release -vvv 
          '';

          installPhase = ''
            mkdir -p $out/bin
            mkdir -p $out/data

            cp target/release/ascend $out/bin/
            cp -r target/site $out/
            wrapProgram $out/bin/ascend --set LEPTOS_SITE_ROOT $out/site --set MOONBOARD_PROBLEMS $out/data/moonboard-problems

            mkdir -p $out/data/moonboard-problems
            cp "moonboard-problems/problems Mini MoonBoard 2020 40.json" $out/data/moonboard-problems/
          '';

          doCheck = false;
        };

      nixosModules.default =
        { config, lib, ... }:
        let
          cfg = config.services.ascend;
        in
        {
          options = {
            services.ascend = {
              enable = lib.mkEnableOption "Ascend service";
              bindAddress = lib.mkOption {
                description = "Bind address (LEPTOS_SITE_ADDR)";
                type = lib.types.string;
                default = "127.0.0.1:1337";
              };
            };
          };
          config = lib.mkIf cfg.enable {
            users.users.ascend = {
              isSystemUser = true;
              home = "/home/ascend";
              group = "ascend";
              description = "Ascend service user";
              createHome = true;
            };
            users.groups.ascend = { };
            systemd.services.ascend = {
              enable = true;
              wantedBy = [ "multi-user.target" ];
              after = [ "network.target" ];
              description = "Start the Ascend service";
              environment = {
                RUST_LOG = "info,ascend=debug";
                LEPTOS_SITE_ADDR = cfg.bindAddress;
              };
              path = [ ];
              serviceConfig = {
                User = "ascend";
                Group = "ascend";
                Type = "simple";
                Restart = "always";
                ExecStart = ''
                  ${self.packages."x86_64-linux".default}/bin/ascend serve
                '';
                WorkingDirectory = "/home/ascend";
              };
            };
          };
        };

      devShells."x86_64-linux".default =
        let
          pkgs = import nixpkgs { system = "x86_64-linux"; };
        in
        basecamp.mkShell pkgs {
          rust.enable = true;
          rust.toolchain.targets = [ "wasm32-unknown-unknown" ];
          rust.toolchain.components.rust-analyzer.nightly = true;

          packages = [
            pkgs.bacon
            pkgs.cargo-leptos
            pkgs.leptosfmt
            pkgs.dart-sass
            pkgs.tailwindcss_4
            pkgs.tailwindcss-language-server

            # For optimizing wasm release builds
            pkgs.binaryen
          ];

          env.RUST_LOG = "info,ascend=debug";
          env.MOONBOARD_PROBLEMS = "moonboard-problems";
          env.LEPTOS_TAILWIND_VERSION = "v4.0.8";
        };
    };
}
