Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
nix: Add flake
Browse files Browse the repository at this point in the history
This adds a nix flake by copying that of https://github.com/typst/typst and
adjusting its usages of typst.

Closes #68.
  • Loading branch information
tingerrr committed Jan 25, 2025
1 parent c39893f commit d429671
Show file tree
Hide file tree
Showing 3 changed files with 281 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Justfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
book-src := justfile_directory() / 'docs' / 'book'

# the cargo variable is used to run `cargo` in the nix dev shell, but
# `cargo +1.80` outside of it
CARGO_1_80 := env('CARGO_1_80', 'cargo +1.80')

[private]
default:
@just --unsorted --list --list-submodules
Expand All @@ -21,10 +25,10 @@ run *args='--release':
ci $RUSTFLAGS='-Dwarnings' $RUSTDOCFLAGS='-Dwarnings':
# FIXME(tinger): See https://github.com/rust-lang/rust/issues/128538 if you get
# high CPU doc tests
cargo +1.80 test --workspace
cargo +1.80 clippy --workspace
cargo +1.80 fmt --all --check
cargo +1.80 doc --workspace --no-deps
{{ CARGO_1_80 }} test --workspace
{{ CARGO_1_80 }} clippy --workspace
{{ CARGO_1_80 }} fmt --all --check
{{ CARGO_1_80 }} doc --workspace --no-deps

# clean all temporary directories and build artifacts
clean:
Expand Down
142 changes: 142 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

131 changes: 131 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# SPDX-License-Identifier: Apache-2.0
# Credits: The Typst Authors

{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
systems.url = "github:nix-systems/default";

crane.url = "github:ipetkov/crane";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
rust-manifest = {
url = "https://static.rust-lang.org/dist/channel-rust-1.80.0.toml";
flake = false;
};
};

outputs = inputs@{ flake-parts, crane, nixpkgs, fenix, rust-manifest, self, ... }: flake-parts.lib.mkFlake { inherit inputs; } {
systems = import inputs.systems;

imports = [
inputs.flake-parts.flakeModules.easyOverlay
];

perSystem = { self', pkgs, lib, system, ... }:
let
cargoToml = lib.importTOML ./Cargo.toml;

pname = "typst-test";
version = cargoToml.workspace.package.version;

rust-toolchain = (fenix.packages.${system}.fromManifestFile rust-manifest).defaultToolchain;

# Crane-based Nix flake configuration.
# Based on https://github.com/ipetkov/crane/blob/master/examples/trunk-workspace/flake.nix
craneLib = (crane.mkLib pkgs).overrideToolchain rust-toolchain;

# Typst-test files to include in the derivation.
# Here we include Rust files.
src = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./Cargo.toml
./Cargo.lock
./rustfmt.toml
./crates
./assets/default-test
];
};

# Typst-test derivation's args, used within crane's derivation
# generation functions.
commonCraneArgs = {
inherit src pname version;

buildInputs = [
pkgs.openssl
] ++ (lib.optionals pkgs.stdenv.isDarwin [
pkgs.darwin.apple_sdk.frameworks.CoreServices
pkgs.libiconv
]);

nativeBuildInputs = [
pkgs.pkg-config
pkgs.openssl.dev
];
};

# Derivation with just the dependencies, so we don't have to keep
# re-building them.
cargoArtifacts = craneLib.buildDepsOnly commonCraneArgs;

typst-test = craneLib.buildPackage (commonCraneArgs // {
inherit cargoArtifacts;

nativeBuildInputs = commonCraneArgs.nativeBuildInputs ++ [
pkgs.installShellFiles
];

GEN_ARTIFACTS = "artifacts";
TYPST_TEST_VERSION =
let
rev = self.shortRev or "dirty";
version = cargoToml.workspace.package.version;
in
"${version} (${rev})";

meta.mainProgram = "typst-test";
});
in
{
formatter = pkgs.nixpkgs-fmt;

packages = {
default = typst-test;
typst-test-dev = self'.packages.default;
};

overlayAttrs = builtins.removeAttrs self'.packages [ "default" ];

apps.default = {
type = "app";
program = lib.getExe typst-test;
};

checks = {
typst-test-fmt = craneLib.cargoFmt commonCraneArgs;
typst-test-clippy = craneLib.cargoClippy (commonCraneArgs // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--workspace -- --deny warnings";
});
typst-test-test = craneLib.cargoTest (commonCraneArgs // {
inherit cargoArtifacts;
cargoTestExtraArgs = "--workspace";
});
};

devShells.default = craneLib.devShell {
checks = self'.checks;
inputsFrom = [ typst-test ];

# see justfile, this allows using cargo with `+1.80` outside the dev
# shell, but without it inside the dev shell
CARGO_1_80 = "cargo";
};
};
};
}

0 comments on commit d429671

Please sign in to comment.