From a84d64d9240b9dec88284671d641fe0ae103ba5a Mon Sep 17 00:00:00 2001 From: Paul-Nicolas Madelaine Date: Thu, 28 Sep 2023 19:55:47 +0200 Subject: [PATCH] nix: refactor --- default.nix | 14 ++++ flake.lock | 10 ++- flake.nix | 58 +++----------- lib/systems.nix | 3 - nix/check-api.nix | 42 ----------- nix/check-formatted.nix | 18 ----- nix/checks/api.nix | 71 +++++++++++++++++ nix/checks/default.nix | 8 ++ nix/checks/formatted.nix | 19 +++++ nix/{nixos/test.nix => checks/nixos.nix} | 14 ++-- nix/devshell.nix | 59 +++++++-------- nix/doc.nix | 11 --- {lib => nix/lib}/default.nix | 4 +- {lib => nix/lib}/dummyWebhook.nix | 0 {lib => nix/lib}/eachSystem.nix | 0 {lib => nix/lib}/github/default.nix | 0 {lib => nix/lib}/github/githubWebhook.nix | 0 {lib => nix/lib}/github/mkGithubJobsets.nix | 0 {lib => nix/lib}/github/mkGithubProject.nix | 0 {lib => nix/lib}/github/mkGithubStatus.nix | 0 {lib => nix/lib}/mkGitJobsets.nix | 0 {lib => nix/lib}/mkProject.nix | 0 nix/lib/systems.nix | 3 + nix/nixos/typhon.nix | 22 +++++- nix/nixpkgs.nix | 8 ++ nix/outputs.nix | 15 ++++ nix/{ => packages}/api-client-test.nix | 0 nix/packages/default.nix | 10 +++ nix/packages/doc.nix | 12 +++ nix/packages/server.nix | 28 +++++++ nix/packages/webapp.nix | 68 +++++++++++++++++ nix/packages/webroot.nix | 43 +++++++++++ nix/rust.nix | 10 +++ nix/server.nix | 19 ----- nix/sources.nix | 1 + nix/systems.nix | 6 ++ {lib => nix}/utils.nix | 12 +-- nix/webapp.nix | 84 --------------------- shell.nix | 1 + 39 files changed, 396 insertions(+), 277 deletions(-) create mode 100644 default.nix delete mode 100644 lib/systems.nix delete mode 100644 nix/check-api.nix delete mode 100644 nix/check-formatted.nix create mode 100644 nix/checks/api.nix create mode 100644 nix/checks/default.nix create mode 100644 nix/checks/formatted.nix rename nix/{nixos/test.nix => checks/nixos.nix} (84%) delete mode 100644 nix/doc.nix rename {lib => nix/lib}/default.nix (68%) rename {lib => nix/lib}/dummyWebhook.nix (100%) rename {lib => nix/lib}/eachSystem.nix (100%) rename {lib => nix/lib}/github/default.nix (100%) rename {lib => nix/lib}/github/githubWebhook.nix (100%) rename {lib => nix/lib}/github/mkGithubJobsets.nix (100%) rename {lib => nix/lib}/github/mkGithubProject.nix (100%) rename {lib => nix/lib}/github/mkGithubStatus.nix (100%) rename {lib => nix/lib}/mkGitJobsets.nix (100%) rename {lib => nix/lib}/mkProject.nix (100%) create mode 100644 nix/lib/systems.nix create mode 100644 nix/nixpkgs.nix create mode 100644 nix/outputs.nix rename nix/{ => packages}/api-client-test.nix (100%) create mode 100644 nix/packages/default.nix create mode 100644 nix/packages/doc.nix create mode 100644 nix/packages/server.nix create mode 100644 nix/packages/webapp.nix create mode 100644 nix/packages/webroot.nix create mode 100644 nix/rust.nix delete mode 100644 nix/server.nix create mode 100644 nix/sources.nix create mode 100644 nix/systems.nix rename {lib => nix}/utils.nix (76%) delete mode 100644 nix/webapp.nix create mode 100644 shell.nix diff --git a/default.nix b/default.nix new file mode 100644 index 00000000..c7d0c267 --- /dev/null +++ b/default.nix @@ -0,0 +1,14 @@ +( + import + ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } + ) + {src = ./.;} +) +.defaultNix diff --git a/flake.lock b/flake.lock index 8ee5a6b0..605f8c7d 100644 --- a/flake.lock +++ b/flake.lock @@ -2,7 +2,9 @@ "nodes": { "crane": { "inputs": { - "flake-compat": "flake-compat", + "flake-compat": [ + "flake-compat" + ], "flake-utils": [ "flake-utils" ], @@ -56,9 +58,8 @@ "type": "github" }, "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" + "id": "flake-utils", + "type": "indirect" } }, "nixpkgs": { @@ -79,6 +80,7 @@ "root": { "inputs": { "crane": "crane", + "flake-compat": "flake-compat", "flake-utils": "flake-utils", "nixpkgs": "nixpkgs", "rust-overlay": "rust-overlay" diff --git a/flake.nix b/flake.nix index a323a61f..19526416 100644 --- a/flake.nix +++ b/flake.nix @@ -2,14 +2,23 @@ description = "Typhon"; inputs = { - flake-utils.url = "github:numtide/flake-utils"; + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; + + flake-utils.url = "flake-utils"; + nixpkgs.url = "nixpkgs/nixos-unstable"; + crane = { url = "github:ipetkov/crane"; + inputs.flake-compat.follows = "flake-compat"; inputs.flake-utils.follows = "flake-utils"; inputs.nixpkgs.follows = "nixpkgs"; inputs.rust-overlay.follows = "rust-overlay"; }; + rust-overlay = { url = "github:oxalica/rust-overlay"; inputs = { @@ -19,50 +28,5 @@ }; }; - outputs = inputs @ { - self, - flake-utils, - nixpkgs, - crane, - rust-overlay, - }: let - lib = import ./lib inputs; - in - flake-utils.lib.eachSystem lib.systems (system: let - pkgs = import nixpkgs { - inherit system; - overlays = [(import rust-overlay)]; - }; - rustToolchain = pkgs.rust-bin.stable.latest.default.override { - targets = ["wasm32-unknown-unknown"]; - }; - craneLib = crane.lib.${system}.overrideToolchain rustToolchain; - - typhon = pkgs.callPackage ./nix/server.nix {inherit craneLib;}; - typhon-webapp = pkgs.callPackage ./nix/webapp.nix {inherit craneLib;}; - typhon-doc = pkgs.callPackage ./nix/doc.nix {}; - typhon-api-client-test = - pkgs.callPackage ./nix/api-client-test.nix {inherit craneLib;}; - typhon-devshell = - pkgs.callPackage ./nix/devshell.nix {inherit rustToolchain;}; - in { - packages = { - inherit typhon typhon-webapp typhon-doc typhon-api-client-test; - default = typhon; - }; - - devShells.default = typhon-devshell; - - checks = { - api = pkgs.callPackage ./nix/check-api.nix { - inherit typhon typhon-api-client-test; - }; - formatted = pkgs.callPackage ./nix/check-formatted.nix {inherit rustToolchain;}; - nixos = pkgs.callPackage ./nix/nixos/test.nix {typhon = self;}; - }; - }) - // { - inherit lib; - nixosModules.default = import ./nix/nixos/typhon.nix self; - }; + outputs = inputs: import ./nix/outputs.nix {sources = inputs;}; } diff --git a/lib/systems.nix b/lib/systems.nix deleted file mode 100644 index 5051e147..00000000 --- a/lib/systems.nix +++ /dev/null @@ -1,3 +0,0 @@ -utils: _: { - systems = utils.inputs.flake-utils.lib.defaultSystems; -} diff --git a/nix/check-api.nix b/nix/check-api.nix deleted file mode 100644 index c982aa3d..00000000 --- a/nix/check-api.nix +++ /dev/null @@ -1,42 +0,0 @@ -{ - bubblewrap, - coreutils, - diesel-cli, - nix, - pkg-config, - sqlite, - stdenv, - typhon, - typhon-api-client-test, -}: -stdenv.mkDerivation { - name = "Test API"; - phases = ["configurePhase" "installPhase"]; - DATABASE_URL = "/tmp/typhon.sqlite"; - configurePhase = '' - export HOME=$(mktemp -d) - mkdir -p ~/.config/nix - echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf - ''; - installPhase = '' - # start Typhon server - typhon -p $(echo -n password | sha256sum | head -c 64) -j null -w "" & - sleep 1 - - # run the test client - PROJECT_DECL="path:${../tests/empty}" typhon-api-client-test - - # kill the server and creates $out - kill %1 && touch $out - ''; - nativeBuildInputs = [ - typhon-api-client-test - typhon - coreutils - bubblewrap - diesel-cli - pkg-config - sqlite - nix - ]; -} diff --git a/nix/check-formatted.nix b/nix/check-formatted.nix deleted file mode 100644 index 637333a6..00000000 --- a/nix/check-formatted.nix +++ /dev/null @@ -1,18 +0,0 @@ -{ - alejandra, - rustToolchain, - stdenv, -}: -stdenv.mkDerivation { - name = "formatted"; - src = ../.; - nativeBuildInputs = [ - alejandra - rustToolchain - ]; - buildPhase = '' - alejandra -c . - cargo fmt --check - ''; - installPhase = "touch $out"; -} diff --git a/nix/checks/api.nix b/nix/checks/api.nix new file mode 100644 index 00000000..52e95c20 --- /dev/null +++ b/nix/checks/api.nix @@ -0,0 +1,71 @@ +{ + sources ? import ../sources.nix, + system ? builtins.currentSystem or "unknown-system", + pkgs ? import ../nixpkgs.nix {inherit sources system;}, + rust ? import ../rust.nix {inherit sources system;}, +}: let + inherit (pkgs) lib; + + inherit (rust) craneLib; + + src = lib.sourceByRegex ../.. [ + "Cargo.toml" + "Cargo.lock" + "typhon.*" + ]; + + cargoToml = ../../typhon-api-client-test/Cargo.toml; + + cargoExtraArgs = "-p typhon-api-client-test"; + + nativeBuildInputs = [ + pkgs.openssl + pkgs.pkg-config + ]; + + cargoArtifacts = craneLib.buildDepsOnly { + inherit src cargoToml cargoExtraArgs nativeBuildInputs; + }; + + typhon-api-client-test = craneLib.buildPackage { + inherit src cargoToml cargoArtifacts cargoExtraArgs nativeBuildInputs; + }; + + typhon = import ../packages/server.nix {inherit sources system;}; +in + pkgs.stdenv.mkDerivation { + name = "Test API"; + phases = ["configurePhase" "installPhase"]; + DATABASE_URL = "/tmp/typhon.sqlite"; + configurePhase = '' + export HOME=$(mktemp -d) + mkdir -p ~/.config/nix + echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf + ''; + installPhase = '' + # start Typhon server + typhon -p $(echo -n password | sha256sum | head -c 64) -j null -w "" & + sleep 1 + + # run the test client + PROJECT_DECL="path:${../../tests/empty}" typhon-api-client-test + + # kill the server and creates $out + kill %1 && touch $out + ''; + nativeBuildInputs = builtins.attrValues { + inherit + typhon + typhon-api-client-test + ; + inherit + (pkgs) + coreutils + bubblewrap + diesel-cli + pkg-config + sqlite + nix + ; + }; + } diff --git a/nix/checks/default.nix b/nix/checks/default.nix new file mode 100644 index 00000000..3a7e10cd --- /dev/null +++ b/nix/checks/default.nix @@ -0,0 +1,8 @@ +{ + sources ? import ../sources.nix, + system ? builtins.currentSystem or "unknown-system", +}: { + api = import ./api.nix {inherit sources system;}; + formatted = import ./formatted.nix {inherit sources system;}; + nixos = import ./nixos.nix {inherit sources system;}; +} diff --git a/nix/checks/formatted.nix b/nix/checks/formatted.nix new file mode 100644 index 00000000..4417590c --- /dev/null +++ b/nix/checks/formatted.nix @@ -0,0 +1,19 @@ +{ + sources ? import ../sources.nix, + system ? builtins.currentSystem or "unknown-system", + pkgs ? import ../nixpkgs.nix {inherit sources system;}, + rust ? import ../rust.nix {inherit sources system;}, +}: +pkgs.stdenv.mkDerivation { + name = "formatted"; + src = ../..; + nativeBuildInputs = [ + pkgs.alejandra + rust.rustToolchain + ]; + buildPhase = '' + alejandra -c . + cargo fmt --check + ''; + installPhase = "touch $out"; +} diff --git a/nix/nixos/test.nix b/nix/checks/nixos.nix similarity index 84% rename from nix/nixos/test.nix rename to nix/checks/nixos.nix index 504457f9..6b4b0e4c 100644 --- a/nix/nixos/test.nix +++ b/nix/checks/nixos.nix @@ -1,18 +1,16 @@ { - typhon, - testers, + sources ? import ../sources.nix, + system ? builtins.currentSystem or "unknown-system", + pkgs ? import ../nixpkgs.nix {inherit sources system;}, + typhon ? import ../nixos/typhon.nix {inherit sources;}, }: -testers.nixosTest ({ - pkgs, - lib, - ... -}: { +pkgs.testers.nixosTest ({pkgs, ...}: { name = "typhon-test"; nodes = { typhon = {...}: { nix.settings.experimental-features = ["nix-command" "flakes"]; - imports = [typhon.nixosModules.default]; + imports = [typhon]; services.typhon = { enable = true; hashedPassword = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"; diff --git a/nix/devshell.nix b/nix/devshell.nix index 2b72709d..419be0a2 100644 --- a/nix/devshell.nix +++ b/nix/devshell.nix @@ -1,42 +1,41 @@ { - bubblewrap, - diesel-cli, - mdbook, - mkShell, - nix, - nodePackages, - nodejs, - openssl, - pkg-config, - rust-analyzer, - rustToolchain, - rustfmt, - sqlite, - trunk, + sources ? import ./sources.nix, + system ? builtins.currentSystem or "unknown-system", + pkgs ? import ./nixpkgs.nix {inherit sources system;}, + rust ? import ./rust.nix {inherit sources system;}, }: -mkShell { +pkgs.mkShell { name = "typhon-devshell"; - packages = [ + packages = builtins.attrValues { # Rust - rustToolchain - rustfmt - rust-analyzer - openssl + inherit (rust) rustToolchain; + inherit + (pkgs) + rustfmt + rust-analyzer + openssl + ; # Typhon server - bubblewrap - diesel-cli - pkg-config - sqlite - nix + inherit + (pkgs) + bubblewrap + diesel-cli + pkg-config + sqlite + nix + ; # Typhon webapp - nodePackages.sass - trunk - nodejs # npm + inherit (pkgs.nodePackages) sass; + inherit + (pkgs) + trunk + nodejs # npm + ; # Documentation - mdbook - ]; + inherit (pkgs) mdbook; + }; DATABASE_URL = "sqlite:typhon.sqlite"; } diff --git a/nix/doc.nix b/nix/doc.nix deleted file mode 100644 index 49a93d99..00000000 --- a/nix/doc.nix +++ /dev/null @@ -1,11 +0,0 @@ -{ - stdenv, - mdbook, -}: -stdenv.mkDerivation { - name = "typhon-doc"; - src = ../doc; - nativeBuildInputs = [mdbook]; - buildPhase = "mdbook build"; - installPhase = "cp -r book $out"; -} diff --git a/lib/default.nix b/nix/lib/default.nix similarity index 68% rename from lib/default.nix rename to nix/lib/default.nix index 9eab9cee..0b51a300 100644 --- a/lib/default.nix +++ b/nix/lib/default.nix @@ -1,5 +1,5 @@ -inputs: let - utils = import ./utils.nix inputs; +{sources ? import ../sources.nix}: let + utils = import ../utils.nix {inherit sources;}; self = utils.importer null [ ./dummyWebhook.nix diff --git a/lib/dummyWebhook.nix b/nix/lib/dummyWebhook.nix similarity index 100% rename from lib/dummyWebhook.nix rename to nix/lib/dummyWebhook.nix diff --git a/lib/eachSystem.nix b/nix/lib/eachSystem.nix similarity index 100% rename from lib/eachSystem.nix rename to nix/lib/eachSystem.nix diff --git a/lib/github/default.nix b/nix/lib/github/default.nix similarity index 100% rename from lib/github/default.nix rename to nix/lib/github/default.nix diff --git a/lib/github/githubWebhook.nix b/nix/lib/github/githubWebhook.nix similarity index 100% rename from lib/github/githubWebhook.nix rename to nix/lib/github/githubWebhook.nix diff --git a/lib/github/mkGithubJobsets.nix b/nix/lib/github/mkGithubJobsets.nix similarity index 100% rename from lib/github/mkGithubJobsets.nix rename to nix/lib/github/mkGithubJobsets.nix diff --git a/lib/github/mkGithubProject.nix b/nix/lib/github/mkGithubProject.nix similarity index 100% rename from lib/github/mkGithubProject.nix rename to nix/lib/github/mkGithubProject.nix diff --git a/lib/github/mkGithubStatus.nix b/nix/lib/github/mkGithubStatus.nix similarity index 100% rename from lib/github/mkGithubStatus.nix rename to nix/lib/github/mkGithubStatus.nix diff --git a/lib/mkGitJobsets.nix b/nix/lib/mkGitJobsets.nix similarity index 100% rename from lib/mkGitJobsets.nix rename to nix/lib/mkGitJobsets.nix diff --git a/lib/mkProject.nix b/nix/lib/mkProject.nix similarity index 100% rename from lib/mkProject.nix rename to nix/lib/mkProject.nix diff --git a/nix/lib/systems.nix b/nix/lib/systems.nix new file mode 100644 index 00000000..1c7a593a --- /dev/null +++ b/nix/lib/systems.nix @@ -0,0 +1,3 @@ +utils: _: { + systems = import ../systems.nix; +} diff --git a/nix/nixos/typhon.nix b/nix/nixos/typhon.nix index 7520e08f..d61fec00 100644 --- a/nix/nixos/typhon.nix +++ b/nix/nixos/typhon.nix @@ -1,17 +1,31 @@ -typhon: { +{sources ? import ../sources.nix}: { config, lib, pkgs, ... }: let - typhonPackages = typhon.packages.${config.nixpkgs.system}; - inherit (lib) mkEnableOption mkIf mkOption types; + typhonPackages = import ../packages { + inherit sources; + inherit (config.nixpkgs) system; + }; + + inherit + (lib) + mkEnableOption + mkIf + mkOption + types + ; + cfg = config.services.typhon; + gcrootsDir = "/nix/var/nix/gcroots/typhon"; + init-execstart = pkgs.writeShellScript "typhon-init" '' [ -e ${gcrootsDir} ] || mkdir ${gcrootsDir} chown typhon:typhon ${gcrootsDir} ''; + typhon-execstart = let protocol = if cfg.https @@ -35,7 +49,7 @@ in { webapp = mkOption { type = types.package; description = "Which webapp to use for the Typhon instance"; - default = typhonPackages.typhon-webapp; + default = typhonPackages.typhon-webroot; }; home = mkOption { type = types.str; diff --git a/nix/nixpkgs.nix b/nix/nixpkgs.nix new file mode 100644 index 00000000..ac68c61e --- /dev/null +++ b/nix/nixpkgs.nix @@ -0,0 +1,8 @@ +{ + sources ? import ./sources.nix, + system ? builtins.currentSystem or "unknown-system", +}: +import sources.nixpkgs { + inherit system; + overlays = [(import sources.rust-overlay)]; +} diff --git a/nix/outputs.nix b/nix/outputs.nix new file mode 100644 index 00000000..0beef2d7 --- /dev/null +++ b/nix/outputs.nix @@ -0,0 +1,15 @@ +{sources ? import ./sources.nix}: let + lib = import ./lib {inherit sources;}; +in { + inherit lib; + + checks = lib.eachSystem (system: import ./checks {inherit sources system;}); + + devShells = lib.eachSystem (system: { + default = import ./devshell.nix {inherit sources system;}; + }); + + packages = lib.eachSystem (system: import ./packages {inherit sources system;}); + + nixosModules.default = import ./nixos/typhon.nix {inherit sources;}; +} diff --git a/nix/api-client-test.nix b/nix/packages/api-client-test.nix similarity index 100% rename from nix/api-client-test.nix rename to nix/packages/api-client-test.nix diff --git a/nix/packages/default.nix b/nix/packages/default.nix new file mode 100644 index 00000000..59ec134e --- /dev/null +++ b/nix/packages/default.nix @@ -0,0 +1,10 @@ +{ + sources ? import ../sources.nix, + system ? builtins.currentSystem or "unknown-system", +}: rec { + default = typhon; + typhon = import ./server.nix {inherit sources system;}; + typhon-webapp = import ./webapp.nix {inherit sources system;}; + typhon-webroot = import ./webroot.nix {inherit sources system;}; + typhon-doc = import ./doc.nix {inherit sources system;}; +} diff --git a/nix/packages/doc.nix b/nix/packages/doc.nix new file mode 100644 index 00000000..1972b226 --- /dev/null +++ b/nix/packages/doc.nix @@ -0,0 +1,12 @@ +{ + sources ? import ../sources.nix, + system ? builtins.currentSystem or "unknown-system", + pkgs ? import ../nixpkgs.nix {inherit sources system;}, +}: +pkgs.stdenv.mkDerivation { + name = "typhon-doc"; + src = ../../doc; + nativeBuildInputs = [pkgs.mdbook]; + buildPhase = "mdbook build"; + installPhase = "cp -r book $out"; +} diff --git a/nix/packages/server.nix b/nix/packages/server.nix new file mode 100644 index 00000000..4b83ea1d --- /dev/null +++ b/nix/packages/server.nix @@ -0,0 +1,28 @@ +{ + sources ? import ../sources.nix, + system ? builtins.currentSystem or "unknown-system", + pkgs ? import ../nixpkgs.nix {inherit sources system;}, + rust ? import ../rust.nix {inherit sources system;}, +}: let + inherit (pkgs) lib; + + inherit (rust) craneLib; + + src = lib.sourceByRegex ../.. [ + "Cargo.toml" + "Cargo.lock" + "typhon.*" + ]; + + cargoToml = ../../typhon/Cargo.toml; + + cargoArtifacts = craneLib.buildDepsOnly {inherit src cargoToml;}; +in + craneLib.buildPackage { + inherit + src + cargoToml + cargoArtifacts + ; + buildInputs = [pkgs.sqlite.dev]; + } diff --git a/nix/packages/webapp.nix b/nix/packages/webapp.nix new file mode 100644 index 00000000..57b195fb --- /dev/null +++ b/nix/packages/webapp.nix @@ -0,0 +1,68 @@ +{ + sources ? import ../sources.nix, + system ? builtins.currentSystem or "unknown-system", + pkgs ? import ../nixpkgs.nix {inherit sources system;}, + rust ? import ../rust.nix {inherit sources system;}, +}: let + inherit (pkgs) lib; + + inherit (rust) craneLib; + + src = lib.sourceByRegex ../.. [ + "Cargo.toml" + "Cargo.lock" + "typhon.*" + ]; + + cargoToml = ../../typhon-webapp/Cargo.toml; + + RUSTFLAGS = "--cfg=web_sys_unstable_apis"; + + cargoArtifacts = craneLib.buildDepsOnly { + inherit src cargoToml RUSTFLAGS; + cargoExtraArgs = "-p typhon-webapp --target wasm32-unknown-unknown"; + doCheck = false; + }; + + nodeDependencies = + (pkgs.callPackage ../../typhon-webapp/npm-nix {}).nodeDependencies; + + inherit + (pkgs) + nodePackages + binaryen + trunk + wasm-bindgen-cli + ; +in + craneLib.buildPackage { + inherit + src + cargoToml + cargoArtifacts + RUSTFLAGS + ; + buildPhaseCargoCommand = '' + ln -s ${nodeDependencies}/lib/node_modules typhon-webapp/node_modules + # See #351 on Trunk + echo "tools.wasm_bindgen = \"$(wasm-bindgen --version | cut -d' ' -f2)\"" >> Trunk.toml + echo "build.public_url = \"WEBROOT\"" >> Trunk.toml + trunk build --release typhon-webapp/index.html + ''; + # we only need to remove references on *.wasm files + doNotRemoveReferencesToVendorDir = true; + installPhaseCommand = '' + cp -r typhon-webapp/dist $out + find "$out/" -name "*.wasm" -print0 | \ + while read -d $'\0' file; do + removeReferencesToVendoredSources $file + done + ''; + nativeBuildInputs = [ + binaryen + nodePackages.sass + trunk + wasm-bindgen-cli + ]; + doCheck = false; + } diff --git a/nix/packages/webroot.nix b/nix/packages/webroot.nix new file mode 100644 index 00000000..a75061fd --- /dev/null +++ b/nix/packages/webroot.nix @@ -0,0 +1,43 @@ +{ + sources ? import ../sources.nix, + system ? builtins.currentSystem or "unknown-system", + pkgs ? import ../nixpkgs.nix {inherit sources system;}, +}: let + webapp = import ./webapp.nix {inherit sources system;}; + + tarball = pkgs.stdenv.mkDerivation { + name = "source.tar.gz"; + src = ../..; + buildPhase = '' + tar -czf $out \ + --sort=name \ + --transform 's/^/typhon\//' \ + . + ''; + }; +in + pkgs.callPackage ( + { + webroot ? "", + baseurl ? "127.0.0.1:8000/api", + https ? false, + }: let + settings = pkgs.writeTextFile { + name = "settings.json"; + text = builtins.toJSON {inherit baseurl https;}; + }; + in + pkgs.stdenv.mkDerivation { + name = "typhon-webroot"; + src = webapp; + buildPhase = '' + substituteInPlace ./index.html --replace "WEBROOT" "${webroot}/" + cp ${settings} settings.json + cp ${tarball} source.tar.gz + ''; + installPhase = '' + mkdir -p $out${webroot} + mv * $out${webroot} + ''; + } + ) {} diff --git a/nix/rust.nix b/nix/rust.nix new file mode 100644 index 00000000..448836c4 --- /dev/null +++ b/nix/rust.nix @@ -0,0 +1,10 @@ +{ + sources ? import ./sources.nix, + system ? builtins.currentSystem or "unknown-system", + pkgs ? import ./nixpkgs.nix {inherit sources system;}, +}: rec { + rustToolchain = pkgs.rust-bin.stable.latest.default.override { + targets = ["wasm32-unknown-unknown"]; + }; + craneLib = sources.crane.lib.${system}.overrideToolchain rustToolchain; +} diff --git a/nix/server.nix b/nix/server.nix deleted file mode 100644 index 06152077..00000000 --- a/nix/server.nix +++ /dev/null @@ -1,19 +0,0 @@ -{ - craneLib, - lib, - sqlite, -}: let - src = lib.sourceByRegex ./.. [ - "Cargo.toml" - "Cargo.lock" - "typhon.*" - ]; - - cargoToml = ../typhon/Cargo.toml; - - cargoArtifacts = craneLib.buildDepsOnly {inherit src cargoToml;}; -in - craneLib.buildPackage { - inherit src cargoToml cargoArtifacts; - buildInputs = [sqlite.dev]; - } diff --git a/nix/sources.nix b/nix/sources.nix new file mode 100644 index 00000000..e99f15cc --- /dev/null +++ b/nix/sources.nix @@ -0,0 +1 @@ +(import ./..).inputs diff --git a/nix/systems.nix b/nix/systems.nix new file mode 100644 index 00000000..a2c57603 --- /dev/null +++ b/nix/systems.nix @@ -0,0 +1,6 @@ +[ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" +] diff --git a/lib/utils.nix b/nix/utils.nix similarity index 76% rename from lib/utils.nix rename to nix/utils.nix index e37afe8f..f3ad69f8 100644 --- a/lib/utils.nix +++ b/nix/utils.nix @@ -1,10 +1,12 @@ -inputs @ {nixpkgs, ...}: let +{sources ? import ./sources.nix}: let + inherit + (sources) + nixpkgs + ; self = rec { - inherit inputs; + lib = sources.nixpkgs.lib; - lib = nixpkgs.lib; - - pkgs = nixpkgs.legacyPackages; + pkgs = sources.nixpkgs.legacyPackages; unionOfDisjoint = x: y: if builtins.intersectAttrs x y == {} diff --git a/nix/webapp.nix b/nix/webapp.nix deleted file mode 100644 index 2082cb71..00000000 --- a/nix/webapp.nix +++ /dev/null @@ -1,84 +0,0 @@ -{ - webroot ? "", - baseurl ? "127.0.0.1:8000/api", - https ? false, - binaryen, - callPackage, - craneLib, - lib, - nodePackages, - stdenv, - trunk, - wasm-bindgen-cli, - writeTextFile, -}: let - src = lib.sourceByRegex ./.. [ - "Cargo.toml" - "Cargo.lock" - "typhon.*" - ]; - - cargoToml = ../typhon-webapp/Cargo.toml; - - RUSTFLAGS = "--cfg=web_sys_unstable_apis"; - - cargoArtifacts = craneLib.buildDepsOnly { - inherit src cargoToml RUSTFLAGS; - cargoExtraArgs = "-p typhon-webapp --target wasm32-unknown-unknown"; - doCheck = false; - }; - - nodeDependencies = - (callPackage ../typhon-webapp/npm-nix {}).nodeDependencies; - - webapp = craneLib.buildPackage { - inherit src cargoToml cargoArtifacts RUSTFLAGS; - buildPhaseCargoCommand = '' - ln -s ${nodeDependencies}/lib/node_modules typhon-webapp/node_modules - # See #351 on Trunk - echo "tools.wasm_bindgen = \"$(wasm-bindgen --version | cut -d' ' -f2)\"" >> Trunk.toml - echo "build.public_url = \"WEBROOT\"" >> Trunk.toml - trunk build --release typhon-webapp/index.html - ''; - # we only need to remove references on *.wasm files - doNotRemoveReferencesToVendorDir = true; - installPhaseCommand = '' - cp -r typhon-webapp/dist $out - find "$out/" -name "*.wasm" -print0 | \ - while read -d $'\0' file; do - removeReferencesToVendoredSources $file - done - ''; - nativeBuildInputs = [binaryen nodePackages.sass trunk wasm-bindgen-cli]; - doCheck = false; - }; - - settings = writeTextFile { - name = "settings.json"; - text = builtins.toJSON {inherit baseurl https;}; - }; - - tarball = stdenv.mkDerivation { - name = "source.tar.gz"; - src = ./..; - buildPhase = '' - tar -czf $out \ - --sort=name \ - --transform 's/^/typhon\//' \ - . - ''; - }; -in - stdenv.mkDerivation { - name = "typhon-webapp"; - src = webapp; - buildPhase = '' - substituteInPlace ./index.html --replace "WEBROOT" "${webroot}/" - cp ${settings} settings.json - cp ${tarball} source.tar.gz - ''; - installPhase = '' - mkdir -p $out${webroot} - mv * $out${webroot} - ''; - } diff --git a/shell.nix b/shell.nix new file mode 100644 index 00000000..33766e2c --- /dev/null +++ b/shell.nix @@ -0,0 +1 @@ +import ./nix/devshell.nix {}