From 5d8685e450acc65a00bd9e4fca0e97ca6655c81c Mon Sep 17 00:00:00 2001 From: frozolotl <44589151+frozolotl@users.noreply.github.com> Date: Thu, 26 Sep 2024 14:38:35 +0200 Subject: [PATCH] Various improvements to nix flake (#5012) --- flake.lock | 98 ++++++++++++++++++++++--------- flake.nix | 168 ++++++++++++++++++++++++++++------------------------- 2 files changed, 160 insertions(+), 106 deletions(-) diff --git a/flake.lock b/flake.lock index 594b8138b..8ef55b72f 100644 --- a/flake.lock +++ b/flake.lock @@ -1,17 +1,12 @@ { "nodes": { "crane": { - "inputs": { - "nixpkgs": [ - "nixpkgs" - ] - }, "locked": { - "lastModified": 1698166613, - "narHash": "sha256-y4rdN4flxRiROqNi1waMYIZj/Fs7L2OrszFk/1ry9vU=", + "lastModified": 1727060013, + "narHash": "sha256-/fC5YlJy4IoAW9GhkJiwyzk0K/gQd9Qi4rRcoweyG9E=", "owner": "ipetkov", "repo": "crane", - "rev": "b7db46f0f1751f7b1d1911f6be7daf568ad5bc65", + "rev": "6b40cc876c929bfe1e3a24bf538ce3b5622646ba", "type": "github" }, "original": { @@ -20,30 +15,52 @@ "type": "github" } }, + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1727245890, + "narHash": "sha256-B4gUhZxqdn24PqL7z7ZuvLOS84HVskhKRByWdgA4/RI=", + "owner": "nix-community", + "repo": "fenix", + "rev": "de3acda8b67b92abeeb35ac236924afd959874ad", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, "flake-parts": { "inputs": { "nixpkgs-lib": "nixpkgs-lib" }, "locked": { - "lastModified": 1696343447, - "narHash": "sha256-B2xAZKLkkeRFG5XcHHSXXcP7To9Xzr59KXeZiRf4vdQ=", + "lastModified": 1726153070, + "narHash": "sha256-HO4zgY0ekfwO5bX0QH/3kJ/h4KvUDFZg8YpkNwIbg1U=", "owner": "hercules-ci", "repo": "flake-parts", - "rev": "c9afaba3dfa4085dbd2ccb38dfade5141e33d9d4", + "rev": "bcef6817a8b2aa20a5a6dbb19b43e63c5bf8619a", "type": "github" }, "original": { - "id": "flake-parts", - "type": "indirect" + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" } }, "nixpkgs": { "locked": { - "lastModified": 1720957393, - "narHash": "sha256-oedh2RwpjEa+TNxhg5Je9Ch6d3W1NKi7DbRO1ziHemA=", + "lastModified": 1726937504, + "narHash": "sha256-bvGoiQBvponpZh8ClUcmJ6QnsNKw0EMrCQJARK3bI1c=", "owner": "nixos", "repo": "nixpkgs", - "rev": "693bc46d169f5af9c992095736e82c3488bf7dbb", + "rev": "9357f4f23713673f310988025d9dc261c20e70c6", "type": "github" }, "original": { @@ -55,30 +72,55 @@ }, "nixpkgs-lib": { "locked": { - "dir": "lib", - "lastModified": 1696019113, - "narHash": "sha256-X3+DKYWJm93DRSdC5M6K5hLqzSya9BjibtBsuARoPco=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "f5892ddac112a1e9b3612c39af1b72987ee5783a", - "type": "github" + "lastModified": 1725233747, + "narHash": "sha256-Ss8QWLXdr2JCBPcYChJhz4xJm+h/xjl4G0c0XlP6a74=", + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/356624c12086a18f2ea2825fed34523d60ccc4e3.tar.gz" }, "original": { - "dir": "lib", - "owner": "NixOS", - "ref": "nixos-unstable", - "repo": "nixpkgs", - "type": "github" + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/356624c12086a18f2ea2825fed34523d60ccc4e3.tar.gz" } }, "root": { "inputs": { "crane": "crane", + "fenix": "fenix", "flake-parts": "flake-parts", "nixpkgs": "nixpkgs", + "rust-manifest": "rust-manifest", "systems": "systems" } }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1727104575, + "narHash": "sha256-lB/ZS0SnHyE8Z3G8DIL/QJPg6w6x5ZhgVO2pBqnz89g=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "3d0343251fe084b335b55c17a52bb4a3527b1bd0", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + }, + "rust-manifest": { + "flake": false, + "locked": { + "narHash": "sha256-3EJkGxF108nDakQ5ZlSQezA8XJMIEk/c9aLDPAUfybM=", + "type": "file", + "url": "https://static.rust-lang.org/dist/channel-rust-1.80.1.toml" + }, + "original": { + "type": "file", + "url": "https://static.rust-lang.org/dist/channel-rust-1.80.1.toml" + } + }, "systems": { "locked": { "lastModified": 1681028828, diff --git a/flake.nix b/flake.nix index 34df58dca..a23fda4ab 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; + cargoToml = lib.importTOML ./Cargo.toml; - pname = "typst"; - version = Cargo-toml.workspace.package.version; + pname = "typst"; + version = cargoToml.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; + rust-toolchain = (fenix.packages.x86_64-linux.fromManifestFile rust-manifest).defaultToolchain; - # 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'' - ]; + # 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 derivation's args, used within crane's derivation generation - # functions. - commonCraneArgs = { - inherit src pname version; + # 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 + ]; + }; - buildInputs = (optionals pkgs.stdenv.isDarwin [ - pkgs.darwin.apple_sdk.frameworks.CoreServices - pkgs.libiconv - ]) ++ [ - pkgs.openssl - ]; + # Typst derivation's args, used within crane's derivation generation + # functions. + commonCraneArgs = { + inherit src pname version; - nativeBuildInputs = [ - pkgs.installShellFiles - pkgs.pkg-config - pkgs.openssl.dev - ]; - }; + buildInputs = [ + pkgs.openssl + ] ++ (lib.optionals pkgs.stdenv.isDarwin [ + pkgs.darwin.apple_sdk.frameworks.CoreServices + pkgs.libiconv + ]); - # 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; + nativeBuildInputs = [ + pkgs.pkg-config + pkgs.openssl.dev + ]; + }; - postInstall = '' - installManPage crates/typst-cli/artifacts/*.1 - installShellCompletion \ - crates/typst-cli/artifacts/typst.{bash,fish} \ - --zsh crates/typst-cli/artifacts/_typst - ''; + # Derivation with just the dependencies, so we don't have to keep + # re-building them. + cargoArtifacts = craneLib.buildDepsOnly commonCraneArgs; - GEN_ARTIFACTS = "artifacts"; - TYPST_VERSION = - let - rev = self.shortRev or "dirty"; - version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).workspace.package.version; - in - "${version} (${rev})"; + typst = craneLib.buildPackage (commonCraneArgs // { + inherit cargoArtifacts; - meta.mainProgram = "typst"; - }); + nativeBuildInputs = commonCraneArgs.nativeBuildInputs ++ [ + pkgs.installShellFiles + ]; - typst = packageFor pkgs; + 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 -- "$@" + '') ]; }; };