From 09442d93eebf176e42d768cbe7b5c31d331dc907 Mon Sep 17 00:00:00 2001 From: jimvdl Date: Mon, 11 Sep 2023 09:57:46 +0200 Subject: Optimize `nix build` times (#2099) --- flake.nix | 159 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 86 insertions(+), 73 deletions(-) (limited to 'flake.nix') diff --git a/flake.nix b/flake.nix index 464ed23a..7430f962 100644 --- a/flake.nix +++ b/flake.nix @@ -1,103 +1,116 @@ { inputs = { - fenix = { - url = "github:nix-community/fenix"; + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + + flake-parts = { + url = "github:hercules-ci/flake-parts"; + inputs.nixpkgs-lib.follows = "nixpkgs"; + }; + + flake-utils.url = "github:numtide/flake-utils"; + + crane = { + url = "github:ipetkov/crane"; inputs.nixpkgs.follows = "nixpkgs"; }; - nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; }; - outputs = { self, fenix, nixpkgs }: + outputs = inputs@{ flake-parts, flake-utils, crane, nixpkgs, ... }: let - inherit (nixpkgs.lib) - genAttrs - importTOML - optionals - sourceByRegex - ; - - eachSystem = f: genAttrs - [ - "aarch64-darwin" - "aarch64-linux" - "x86_64-darwin" - "x86_64-linux" - ] - (system: f nixpkgs.legacyPackages.${system}); - + # Generate the typst package for the given nixpkgs instance. packageFor = pkgs: let - rust = fenix.packages.${pkgs.stdenv.hostPlatform.system}.minimal.toolchain; - rustPlatform = pkgs.makeRustPlatform { - cargo = rust; - rustc = rust; - }; - in - rustPlatform.buildRustPackage { + inherit (nixpkgs.lib) + importTOML + optionals + sourceByRegex + ; + Cargo-toml = importTOML ./Cargo.toml; + pname = "typst"; - inherit ((importTOML ./Cargo.toml).workspace.package) version; + 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, assets and tests. src = sourceByRegex ./. [ "(assets|crates|tests)(/.*)?" ''Cargo\.(toml|lock)'' ''build\.rs'' ]; - cargoLock = { - lockFile = ./Cargo.lock; - allowBuiltinFetchGit = true; + # 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 + ]; + + nativeBuildInputs = [ pkgs.installShellFiles ]; }; - nativeBuildInputs = [ - pkgs.installShellFiles - ]; + # Derivation with just the dependencies, so we don't have to keep + # re-building them. + cargoArtifacts = craneLib.buildDepsOnly commonCraneArgs; - buildInputs = optionals pkgs.stdenv.isDarwin [ - pkgs.darwin.apple_sdk.frameworks.CoreServices - ]; + typst = craneLib.buildPackage (commonCraneArgs // { + inherit cargoArtifacts; - postInstall = '' - installManPage crates/typst-cli/artifacts/*.1 - installShellCompletion \ - crates/typst-cli/artifacts/typst.{bash,fish} \ - --zsh crates/typst-cli/artifacts/_typst - ''; + postInstall = '' + installManPage crates/typst-cli/artifacts/*.1 + installShellCompletion \ + crates/typst-cli/artifacts/typst.{bash,fish} \ + --zsh crates/typst-cli/artifacts/_typst + ''; - GEN_ARTIFACTS = "artifacts"; - }; + GEN_ARTIFACTS = "artifacts"; + }); + in + typst; in - { - devShells = eachSystem (pkgs: { - default = pkgs.mkShell { - packages = - let - fenix' = fenix.packages.${pkgs.stdenv.hostPlatform.system}; - in - [ - (fenix'.default.withComponents [ - "cargo" - "clippy" - "rustc" - "rustfmt" - ]) - fenix'.rust-analyzer - ]; + flake-parts.lib.mkFlake { inherit inputs; } { + systems = [ + "aarch64-darwin" + "aarch64-linux" + "x86_64-darwin" + "x86_64-linux" + ]; - buildInputs = optionals pkgs.stdenv.isDarwin [ - pkgs.darwin.apple_sdk.frameworks.CoreServices - pkgs.libiconv - ]; + flake = { + overlays.default = _: prev: { + typst-dev = packageFor prev; }; - }); + }; - formatter = eachSystem (pkgs: pkgs.nixpkgs-fmt); + perSystem = { pkgs, ... }: + let + inherit (pkgs) lib; + typst = packageFor pkgs; + in + { + packages.default = typst; - overlays.default = _: prev: { - typst-dev = packageFor prev; - }; + apps.default = flake-utils.lib.mkApp { + drv = typst; + }; + + devShells.default = pkgs.mkShell { + packages = with pkgs; [ + rustc + cargo + ]; - packages = eachSystem (pkgs: { - default = packageFor pkgs; - }); + buildInputs = lib.optionals pkgs.stdenv.isDarwin [ + pkgs.darwin.apple_sdk.frameworks.CoreServices + pkgs.libiconv + ]; + }; + }; }; } -- cgit v1.2.3