Skip to content

Commit

Permalink
Merge pull request #600 from tweag/erin/flake-devshell
Browse files Browse the repository at this point in the history
Adds an explicit devShell to our flake
  • Loading branch information
ErinvanderVeen committed Aug 24, 2023
2 parents e30432a + bdf79c9 commit 577fe94
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 121 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
node_modules
target
/result
/.envrc
/.direnv
*.profraw
186 changes: 98 additions & 88 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{ pkgs, nixpkgs, system, advisory-db, crane, rust-overlay, nix-filter }:
let
rustPkgs = import nixpkgs {
inherit system;
overlays = [ (import rust-overlay) ];
};

{
pkgs,
system,
advisory-db,
crane,
rust-overlay,
nix-filter,
}: let
wasmRustVersion = "1.67.1";
wasmTarget = "wasm32-unknown-unknown";

rustWithWasmTarget = rustPkgs.rust-bin.stable.${wasmRustVersion}.default.override {
targets = [ wasmTarget ];
rustWithWasmTarget = pkgs.rust-bin.stable.${wasmRustVersion}.default.override {
targets = [wasmTarget];
};

craneLib = crane.mkLib pkgs;
Expand All @@ -32,91 +33,100 @@ let
];
};

nativeBuildInputs = [
pkgs.binaryen pkgs.wasm-bindgen-cli
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
pkgs.libiconv
];
nativeBuildInputs = with pkgs;
[
binaryen
wasm-bindgen-cli
]
++ lib.optionals stdenv.isDarwin [
libiconv
];
};

cargoArtifacts = craneLib.buildDepsOnly (commonArgs);
cargoArtifacts = craneLib.buildDepsOnly commonArgs;

# NB: we don't need to overlay our custom toolchain for the *entire*
# pkgs (which would require rebuidling anything else which uses rust).
# Instead, we just want to update the scope that crane will use by appending
# our specific toolchain there.
craneLibWasm = craneLib.overrideToolchain rustWithWasmTarget;
in
{
clippy = craneLib.cargoClippy (commonArgs // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "-- --deny warnings";
});

clippy-wasm = craneLibWasm.cargoClippy (commonArgs // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "-p topiary-playground --target ${wasmTarget} -- --deny warnings";
});

fmt = craneLib.cargoFmt (commonArgs);

audit = craneLib.cargoAudit (commonArgs // {
inherit advisory-db;
});

benchmark = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
cargoTestCommand = "cargo bench --profile release";
});

topiary-lib = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
pname = "topiary-lib";
cargoExtraArgs = "-p topiary";
});

topiary-cli = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
pname = "topiary";
cargoExtraArgs = "-p topiary-cli";
postInstall = ''
install -Dm444 languages/* -t $out/share/languages
'';

# Set TOPIARY_LANGUAGE_DIR to the Nix store
# for the build
TOPIARY_LANGUAGE_DIR = "${placeholder "out"}/share/languages";

# Set TOPIARY_LANGUAGE_DIR to the working directory
# in a development shell
shellHook = ''
export TOPIARY_LANGUAGE_DIR=$PWD/languages
'';
});

topiary-playground = craneLibWasm.buildPackage (commonArgs // {
inherit cargoArtifacts;
pname = "topiary-playground";
cargoExtraArgs = "-p topiary-playground --no-default-features --target ${wasmTarget}";

# Tests currently need to be run via `cargo wasi` which
# isn't packaged in nixpkgs yet...
doCheck = false;

postInstall = ''
echo 'Removing unneeded dir'
rm -rf $out/lib
echo 'Running wasm-bindgen'
wasm-bindgen --version
wasm-bindgen --target web --out-dir $out target/wasm32-unknown-unknown/release/topiary_playground.wasm;
echo 'Running wasm-opt'
wasm-opt --version
wasm-opt -Oz -o $out/output.wasm $out/topiary_playground_bg.wasm
echo 'Overwriting topiary_playground_bg.wasm with the optimized file'
mv $out/output.wasm $out/topiary_playground_bg.wasm
echo 'Extracting custom build outputs'
export LANGUAGES_EXPORT="$(ls -t target/wasm32-unknown-unknown/release/build/topiary-playground-*/out/languages_export.ts | head -1)"
cp $LANGUAGES_EXPORT $out/
'';
});
in {
clippy = craneLib.cargoClippy (commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "-- --deny warnings";
});

clippy-wasm = craneLibWasm.cargoClippy (commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "-p topiary-playground --target ${wasmTarget} -- --deny warnings";
});

fmt = craneLib.cargoFmt commonArgs;

audit = craneLib.cargoAudit (commonArgs
// {
inherit advisory-db;
});

benchmark = craneLib.buildPackage (commonArgs
// {
inherit cargoArtifacts;
cargoTestCommand = "cargo bench --profile release";
});

topiary-lib = craneLib.buildPackage (commonArgs
// {
inherit cargoArtifacts;
pname = "topiary-lib";
cargoExtraArgs = "-p topiary";
});

topiary-cli = craneLib.buildPackage (commonArgs
// {
inherit cargoArtifacts;
pname = "topiary";
cargoExtraArgs = "-p topiary-cli";
postInstall = ''
install -Dm444 languages/* -t $out/share/languages
'';

# Set TOPIARY_LANGUAGE_DIR to the Nix store
# for the build
TOPIARY_LANGUAGE_DIR = "${placeholder "out"}/share/languages";

# Set TOPIARY_LANGUAGE_DIR to the working directory
# in a development shell
shellHook = ''
export TOPIARY_LANGUAGE_DIR=$PWD/languages
'';
});

topiary-playground = craneLibWasm.buildPackage (commonArgs
// {
inherit cargoArtifacts;
pname = "topiary-playground";
cargoExtraArgs = "-p topiary-playground --no-default-features --target ${wasmTarget}";

# Tests currently need to be run via `cargo wasi` which
# isn't packaged in nixpkgs yet...
doCheck = false;

postInstall = ''
echo 'Removing unneeded dir'
rm -rf $out/lib
echo 'Running wasm-bindgen'
wasm-bindgen --version
wasm-bindgen --target web --out-dir $out target/wasm32-unknown-unknown/release/topiary_playground.wasm;
echo 'Running wasm-opt'
wasm-opt --version
wasm-opt -Oz -o $out/output.wasm $out/topiary_playground_bg.wasm
echo 'Overwriting topiary_playground_bg.wasm with the optimized file'
mv $out/output.wasm $out/topiary_playground_bg.wasm
echo 'Extracting custom build outputs'
export LANGUAGES_EXPORT="$(ls -t target/wasm32-unknown-unknown/release/build/topiary-playground-*/out/languages_export.ts | head -1)"
cp $LANGUAGES_EXPORT $out/
'';
});
}
67 changes: 35 additions & 32 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,45 +29,48 @@
nix-filter.url = "github:numtide/nix-filter";
};

outputs = inputs: with inputs;
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
code = pkgs.callPackage ./. { inherit nixpkgs system advisory-db crane rust-overlay nix-filter; };
in
{
packages = with code; {
inherit topiary-playground;
default = topiary-cli;
};
outputs = inputs:
with inputs;
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = import nixpkgs {
inherit system;
overlays = [rust-overlay.overlays.default];
};
code = pkgs.callPackage ./default.nix {inherit advisory-db crane rust-overlay nix-filter;};
in {
packages = with code; {
inherit topiary-playground;
default = topiary-cli;
};

checks = {
inherit (code) clippy clippy-wasm fmt topiary-lib topiary-cli topiary-playground audit benchmark;
checks = {
inherit (code) clippy clippy-wasm fmt topiary-lib topiary-cli topiary-playground audit benchmark;

## Check that the `lib.pre-commit-hook` output builds/evaluates
## correctly. `deepSeq e1 e2` evaluates `e1` strictly in depth before
## returning `e2`. We use this trick because checks need to be
## derivations, which `lib.pre-commit-hook` is not.
pre-commit-hook = builtins.deepSeq self.lib.${system}.pre-commit-hook pkgs.hello;
};
## Check that the `lib.pre-commit-hook` output builds/evaluates
## correctly. `deepSeq e1 e2` evaluates `e1` strictly in depth before
## returning `e2`. We use this trick because checks need to be
## derivations, which `lib.pre-commit-hook` is not.
pre-commit-hook = builtins.deepSeq self.lib.${system}.pre-commit-hook pkgs.hello;
};

## For easy use in https://github.com/cachix/pre-commit-hooks.nix
lib.pre-commit-hook = {
enable = true;
name = "topiary";
description = "A general code formatter based on tree-sitter.";
entry =
let
devShells.default = pkgs.callPackage ./shell.nix {checks = self.checks.${system};};

## For easy use in https://github.com/cachix/pre-commit-hooks.nix
lib.pre-commit-hook = {
enable = true;
name = "topiary";
description = "A general code formatter based on tree-sitter.";
entry = let
topiary-inplace = pkgs.writeShellApplication {
name = "topiary-inplace";
text = ''
${code.topiary-cli}/bin/topiary --in-place --input-files "$@";
'';
};
in
"${topiary-inplace}/bin/topiary-inplace";
types = [ "text" ];
};
}
);
in "${topiary-inplace}/bin/topiary-inplace";
types = ["text"];
};
}
);
}
14 changes: 14 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Allows `nix-shell` without having to go trough the trouble of pinning the same
# version as is done by the flake.
{
pkgs ? import <nixpkgs> {},
checks ? {},
}:
pkgs.mkShell {
inputsFrom = builtins.attrValues checks;

buildInputs = with pkgs; [
cargo-flamegraph
rust-analyzer
];
}

0 comments on commit 577fe94

Please sign in to comment.