summaryrefslogtreecommitdiff
path: root/flake.nix
diff options
context:
space:
mode:
authorfrozolotl <44589151+frozolotl@users.noreply.github.com>2024-09-26 14:38:35 +0200
committerGitHub <noreply@github.com>2024-09-26 12:38:35 +0000
commit5d8685e450acc65a00bd9e4fca0e97ca6655c81c (patch)
tree38f931cd760894bd8f925b684d2e20e43d3990fe /flake.nix
parentd8cdc66c57fa2d6a2dbe4b54a34842cdfd4cd393 (diff)
Various improvements to nix flake (#5012)
Diffstat (limited to 'flake.nix')
-rw-r--r--flake.nix186
1 files changed, 99 insertions, 87 deletions
diff --git a/flake.nix b/flake.nix
index 34df58dc..a23fda4a 100644
--- a/flake.nix
+++ b/flake.nix
@@ -1,16 +1,21 @@
{
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";
+ crane.url = "github:ipetkov/crane";
+ fenix = {
+ url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
-
- systems.url = "github:nix-systems/default";
+ rust-manifest = {
+ url = "https://static.rust-lang.org/dist/channel-rust-1.80.1.toml";
+ flake = false;
+ };
};
- outputs = inputs@{ flake-parts, crane, nixpkgs, self, ... }: flake-parts.lib.mkFlake { inherit inputs; } {
+ outputs = inputs@{ flake-parts, crane, nixpkgs, fenix, rust-manifest, self, ... }: flake-parts.lib.mkFlake { inherit inputs; } {
systems = import inputs.systems;
imports = [
@@ -19,76 +24,77 @@
perSystem = { self', pkgs, lib, ... }:
let
- # Generate the typst package for the given nixpkgs instance.
- packageFor = pkgs:
- let
- inherit (lib)
- importTOML
- optionals
- sourceByRegex
- ;
- Cargo-toml = importTOML ./Cargo.toml;
-
- pname = "typst";
- version = Cargo-toml.workspace.package.version;
-
- # Crane-based Nix flake configuration.
- # Based on https://github.com/ipetkov/crane/blob/master/examples/trunk-workspace/flake.nix
- craneLib = crane.mkLib pkgs;
-
- # Typst files to include in the derivation.
- # Here we include Rust files, docs and tests.
- src = sourceByRegex ./. [
- "(docs|crates|tests)(/.*)?"
- ''Cargo\.(toml|lock)''
- ''build\.rs''
- ];
-
- # Typst derivation's args, used within crane's derivation generation
- # functions.
- commonCraneArgs = {
- inherit src pname version;
-
- buildInputs = (optionals pkgs.stdenv.isDarwin [
- pkgs.darwin.apple_sdk.frameworks.CoreServices
- pkgs.libiconv
- ]) ++ [
- pkgs.openssl
- ];
-
- nativeBuildInputs = [
- pkgs.installShellFiles
- 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;
- in
- craneLib.buildPackage (commonCraneArgs // {
- inherit cargoArtifacts;
+ cargoToml = lib.importTOML ./Cargo.toml;
+
+ pname = "typst";
+ version = cargoToml.workspace.package.version;
+
+ rust-toolchain = (fenix.packages.x86_64-linux.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 files to include in the derivation.
+ # Here we include Rust files, docs and tests.
+ src = lib.fileset.toSource {
+ root = ./.;
+ fileset = lib.fileset.unions [
+ ./Cargo.toml
+ ./Cargo.lock
+ ./rustfmt.toml
+ ./crates
+ ./docs
+ ./tests
+ ];
+ };
- postInstall = ''
- installManPage crates/typst-cli/artifacts/*.1
- installShellCompletion \
- crates/typst-cli/artifacts/typst.{bash,fish} \
- --zsh crates/typst-cli/artifacts/_typst
- '';
-
- GEN_ARTIFACTS = "artifacts";
- TYPST_VERSION =
- let
- rev = self.shortRev or "dirty";
- version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).workspace.package.version;
- in
- "${version} (${rev})";
-
- meta.mainProgram = "typst";
- });
+ # Typst 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
+ ];
+ };
- typst = packageFor pkgs;
+ # Derivation with just the dependencies, so we don't have to keep
+ # re-building them.
+ cargoArtifacts = craneLib.buildDepsOnly commonCraneArgs;
+
+ typst = craneLib.buildPackage (commonCraneArgs // {
+ inherit cargoArtifacts;
+
+ nativeBuildInputs = commonCraneArgs.nativeBuildInputs ++ [
+ pkgs.installShellFiles
+ ];
+
+ postInstall = ''
+ installManPage crates/typst-cli/artifacts/*.1
+ installShellCompletion \
+ crates/typst-cli/artifacts/typst.{bash,fish} \
+ --zsh crates/typst-cli/artifacts/_typst
+ '';
+
+ GEN_ARTIFACTS = "artifacts";
+ TYPST_VERSION =
+ let
+ rev = self.shortRev or "dirty";
+ version = cargoToml.workspace.package.version;
+ in
+ "${version} (${rev})";
+
+ meta.mainProgram = "typst";
+ });
in
{
formatter = pkgs.nixpkgs-fmt;
@@ -105,22 +111,28 @@
program = lib.getExe typst;
};
- devShells.default = pkgs.mkShell {
- packages = with pkgs; [
- rustc
- cargo
- ];
+ checks = {
+ typst-fmt = craneLib.cargoFmt commonCraneArgs;
+ typst-clippy = craneLib.cargoClippy (commonCraneArgs // {
+ inherit cargoArtifacts;
+ cargoClippyExtraArgs = "--workspace -- --deny warnings";
+ });
+ typst-test = craneLib.cargoTest (commonCraneArgs // {
+ inherit cargoArtifacts;
+ cargoTestExtraArgs = "--workspace";
+ });
+ };
- buildInputs = (lib.optionals pkgs.stdenv.isDarwin [
- pkgs.darwin.apple_sdk.frameworks.CoreServices
- pkgs.libiconv
- ]) ++ [
- pkgs.openssl
- ];
+ devShells.default = craneLib.devShell {
+ checks = self'.checks;
+ inputsFrom = [ typst ];
- nativeBuildInputs = [
- pkgs.pkg-config
- pkgs.openssl.dev
+ packages = [
+ # A script for quickly running tests.
+ # See https://github.com/typst/typst/blob/main/tests/README.md#making-an-alias
+ (pkgs.writeShellScriptBin "testit" ''
+ cargo test --workspace --test tests -- "$@"
+ '')
];
};
};