Skip to content

Commit

Permalink
nix: restructure flake
Browse files Browse the repository at this point in the history
  • Loading branch information
pnmadelaine committed Jul 10, 2023
1 parent 556fc66 commit 6adfde0
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 161 deletions.
179 changes: 22 additions & 157 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,181 +21,46 @@
outputs = { self, flake-utils, nixpkgs, crane, rust-overlay }:
flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
let

pkgs = import nixpkgs {
inherit system;
overlays = [ (import rust-overlay) ];
};
src = pkgs.lib.sourceByRegex ./. [
"Cargo.toml"
"Cargo.lock"
"typhon.*"
"typhon-types.*"
"typhon-webapp.*"
];
rustToolchain = pkgs.rust-bin.stable.latest.default.override {
targets = [ "wasm32-unknown-unknown" ];
};
craneLib = crane.lib.${system}.overrideToolchain rustToolchain;
typhon = let
cargoToml = ./typhon/Cargo.toml;
cargoArtifacts = craneLib.buildDepsOnly { inherit src cargoToml; };
in craneLib.buildPackage {
inherit src cargoToml cargoArtifacts;
buildInputs = [ pkgs.sqlite.dev ];
};
typhon-api-client-test = let
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;
};
in craneLib.buildPackage {
inherit src cargoToml cargoArtifacts cargoExtraArgs nativeBuildInputs;
};
typhon-webapp = let
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;
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 = with pkgs; [
trunk
wasm-bindgen-cli
binaryen
nodePackages.sass
];
doCheck = false;
};
in pkgs.callPackage ({ stdenv, writeTextFile, webroot ? ""
, baseurl ? "127.0.0.1:8000/api", https ? false }:
let
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}
'';
}) { };
typhon-doc = pkgs.stdenv.mkDerivation {
name = "typhon-doc";
src = ./doc;
nativeBuildInputs = [ pkgs.mdbook ];
buildPhase = "mdbook build";
installPhase = "cp -r book $out";
};

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; };
typhon-actions = import ./nix/actions { inherit pkgs; };

in {

packages = {
inherit typhon typhon-webapp typhon-doc typhon-api-client-test;
default = typhon;
};
devShells.default = pkgs.mkShell {
name = "typhon-shell";
packages = [
# Rust
pkgs.rustfmt
pkgs.rust-analyzer
pkgs.openssl
rustToolchain

# Typhon server
pkgs.bubblewrap
pkgs.diesel-cli
pkgs.pkg-config
pkgs.sqlite
pkgs.nix
devShells.default = typhon-devshell;

# Typhon webapp
pkgs.nodePackages.sass
pkgs.trunk
pkgs.nodejs # npm

# Documentation
pkgs.mdbook
];
DATABASE_URL = "sqlite:typhon.sqlite";
};
checks = {
api = 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 = [
typhon-api-client-test
typhon
pkgs.coreutils
pkgs.bubblewrap
pkgs.diesel-cli
pkgs.pkg-config
pkgs.sqlite
pkgs.nix
];
};
nixos = import ./nixos/test.nix {
inherit system nixpkgs;
typhon = self;
api = pkgs.callPackage ./nix/check-api.nix {
inherit typhon typhon-api-client-test;
};
nixos = pkgs.callPackage ./nix/nixos/test.nix { typhon = self; };
};
actions = import ./actions { inherit pkgs; };

actions = typhon-actions;

}) // {
nixosModules.default = import ./nixos/typhon.nix self;

nixosModules.default = import ./nix/nixos/typhon.nix self;

};
}
File renamed without changes.
24 changes: 24 additions & 0 deletions nix/api-client-test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ craneLib, lib, openssl, pkg-config }:

let

src = lib.sourceByRegex ./.. [
"Cargo.toml"
"Cargo.lock"
"typhon.*"
"typhon-types.*"
];

cargoToml = ../typhon/api-client-test/Cargo.toml;

cargoExtraArgs = "-p typhon-api-client-test";

nativeBuildInputs = [ openssl pkg-config ];

cargoArtifacts = craneLib.buildDepsOnly {
inherit src cargoToml cargoExtraArgs nativeBuildInputs;
};

in craneLib.buildPackage {
inherit src cargoToml cargoArtifacts cargoExtraArgs nativeBuildInputs;
}
34 changes: 34 additions & 0 deletions nix/check-api.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{ typhon, typhon-api-client-test, stdenv, coreutils, bubblewrap, diesel-cli
, pkg-config, sqlite, nix }:

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
];
}
29 changes: 29 additions & 0 deletions nix/devshell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{ rustToolchain, mkShell, rustfmt, rust-analyzer, openssl, bubblewrap
, diesel-cli, pkg-config, sqlite, nix, nodePackages, trunk, nodejs, mdbook }:

mkShell {
name = "typhon-devshell";
packages = [
# Rust
rustToolchain
rustfmt
rust-analyzer
openssl

# Typhon server
bubblewrap
diesel-cli
pkg-config
sqlite
nix

# Typhon webapp
nodePackages.sass
trunk
nodejs # npm

# Documentation
mdbook
];
DATABASE_URL = "sqlite:typhon.sqlite";
}
9 changes: 9 additions & 0 deletions nix/doc.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ stdenv, mdbook }:

stdenv.mkDerivation {
name = "typhon-doc";
src = ../doc;
nativeBuildInputs = [ mdbook ];
buildPhase = "mdbook build";
installPhase = "cp -r book $out";
}
9 changes: 5 additions & 4 deletions nixos/test.nix → nix/nixos/test.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{ system, nixpkgs, typhon }:
{ typhon, testers }:

testers.nixosTest ({ pkgs, lib, ... }: {

import "${nixpkgs}/nixos/tests/make-test-python.nix" ({ pkgs, lib, ... }: {
name = "typhon-test";

nodes = {
Expand All @@ -22,7 +23,7 @@ import "${nixpkgs}/nixos/tests/make-test-python.nix" ({ pkgs, lib, ... }: {
let
curl = "${pkgs.curl}/bin/curl -sf -H 'token: hello'";
url = "http://127.0.0.1/typhon/api";
flake = "path:${../tests/empty}";
flake = "path:${../../tests/empty}";
in ''
typhon.start()
typhon.wait_for_unit("default.target")
Expand Down Expand Up @@ -52,4 +53,4 @@ import "${nixpkgs}/nixos/tests/make-test-python.nix" ({ pkgs, lib, ... }: {
typhon.fail("${curl} ${url}/projects/test/jobsets/main/evaluations/2")
'';

}) { inherit system; }
})
File renamed without changes.
19 changes: 19 additions & 0 deletions nix/server.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{ craneLib, lib, sqlite }:

let

src = lib.sourceByRegex ./.. [
"Cargo.toml"
"Cargo.lock"
"typhon.*"
"typhon-types.*"
];

cargoToml = ../typhon/Cargo.toml;

cargoArtifacts = craneLib.buildDepsOnly { inherit src cargoToml; };

in craneLib.buildPackage {
inherit src cargoToml cargoArtifacts;
buildInputs = [ sqlite.dev ];
}
Loading

0 comments on commit 6adfde0

Please sign in to comment.