diff --git a/.gitignore b/.gitignore index 3f4e43e..8400b8c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /site/target/ /site/dist/ +result diff --git a/README.md b/README.md new file mode 100644 index 0000000..c0b32ca --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# iana-angl site + +Build the static Leptos/Trunk site: + +```sh +nix build .#iana-angl +``` + +## NixOS integration + +Add this flake as an input, import its module, and provide the built package and +virtual host domain: + +```nix +{ + inputs.iana-angl.url = "path:/home/yukkop/pj/iana-angl"; + + outputs = { nixpkgs, ... }@inputs: { + nixosConfigurations.my-host = nixpkgs.lib.nixosSystem { + system = "x86_64-linux"; + modules = [ + inputs.iana-angl.nixosModules.iana-angl + ({ pkgs, ... }: { + services.iana-angl = { + enable = true; + package = inputs.iana-angl.packages.${pkgs.system}.iana-angl; + domain = "lessons.example.com"; + }; + }) + ]; + }; + }; +} +``` + +The module enables nginx, serves the package as a static SPA, and falls back to +`index.html` for client-side routes. Configure TLS, ACME, firewall ports, and +reverse-proxy policy separately in the consuming system. diff --git a/flake.nix b/flake.nix index fb637b6..78d728e 100644 --- a/flake.nix +++ b/flake.nix @@ -21,6 +21,68 @@ forEachSystem = f: nixpkgs.lib.genAttrs systems f; in { + packages = forEachSystem (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ (import rust-overlay) ]; + }; + + rustToolchain = pkgs.rust-bin.stable.latest.default.override { + targets = [ "wasm32-unknown-unknown" ]; + }; + + wasmBindgenCli = pkgs.wasm-bindgen-cli.overrideAttrs (_: rec { + version = "0.2.128"; + src = pkgs.fetchCrate { + pname = "wasm-bindgen-cli"; + inherit version; + hash = "sha256-a7lcXJnnZkYReja+iUO7NqqrWyv3toxnUgQb8s4IS5s="; + }; + cargoDeps = pkgs.rustPlatform.fetchCargoVendor { + inherit src; + hash = "sha256-R1Tas33Ursy8kqsxguAkG0ZhNed2n5uFTAhw1l2qlLY="; + }; + }); + in + { + iana-angl = pkgs.stdenv.mkDerivation { + pname = "iana-angl"; + version = "0.1.0"; + + src = pkgs.lib.cleanSourceWith { + src = ./site; + filter = path: type: + let + name = builtins.baseNameOf path; + in + name != "target" && name != "dist"; + }; + + cargoDeps = pkgs.rustPlatform.importCargoLock { + lockFile = ./site/Cargo.lock; + }; + + nativeBuildInputs = [ + pkgs.rustPlatform.cargoSetupHook + rustToolchain + pkgs.trunk + wasmBindgenCli + pkgs.binaryen + pkgs.pkg-config + ]; + + buildPhase = '' + runHook preBuild + export HOME="$TMPDIR" + trunk build --release --dist "$out" + runHook postBuild + ''; + + installPhase = "true"; + }; + }); + devShells = forEachSystem (system: let pkgs = import nixpkgs { @@ -50,5 +112,7 @@ ''; }; }); + + nixosModules = import ./nixos/module; }; } diff --git a/nixos/module/default.nix b/nixos/module/default.nix new file mode 100644 index 0000000..4bc57b8 --- /dev/null +++ b/nixos/module/default.nix @@ -0,0 +1,7 @@ +let + iana-angl = import ./iana-angl; +in +{ + inherit iana-angl; + default = iana-angl; +} diff --git a/nixos/module/iana-angl/default.nix b/nixos/module/iana-angl/default.nix new file mode 100644 index 0000000..05b2d84 --- /dev/null +++ b/nixos/module/iana-angl/default.nix @@ -0,0 +1,3 @@ +{ + imports = [ ./static-nginx.nix ]; +} diff --git a/nixos/module/iana-angl/static-nginx.nix b/nixos/module/iana-angl/static-nginx.nix new file mode 100644 index 0000000..56ece09 --- /dev/null +++ b/nixos/module/iana-angl/static-nginx.nix @@ -0,0 +1,29 @@ +{ config, lib, ... }: +let + cfg = config.services.iana-angl; +in +{ + options.services.iana-angl = { + enable = lib.mkEnableOption "iana-angl static site"; + + package = lib.mkOption { + type = lib.types.package; + description = "Static iana-angl site package to serve."; + }; + + domain = lib.mkOption { + type = lib.types.strMatching ".+"; + description = "Nonempty nginx virtual host domain for iana-angl."; + }; + }; + + config = lib.mkIf cfg.enable { + services.nginx = { + enable = true; + virtualHosts.${cfg.domain}.locations."/".extraConfig = '' + alias ${cfg.package}/; + try_files $uri $uri/ /index.html; + ''; + }; + }; +}