Optimize nix build times (#2099)

This commit is contained in:
jimvdl 2023-09-11 09:57:46 +02:00 committed by GitHub
parent 073effc740
commit 09442d93ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 229 additions and 102 deletions

160
flake.lock generated
View File

@ -1,33 +1,107 @@
{ {
"nodes": { "nodes": {
"fenix": { "crane": {
"inputs": { "inputs": {
"flake-compat": "flake-compat",
"flake-utils": "flake-utils",
"nixpkgs": [ "nixpkgs": [
"nixpkgs" "nixpkgs"
], ],
"rust-analyzer-src": "rust-analyzer-src" "rust-overlay": "rust-overlay"
}, },
"locked": { "locked": {
"lastModified": 1686291735, "lastModified": 1693787605,
"narHash": "sha256-mpq2m6TN3ImqqUqA4u93NvkZu5vH//3spqjmPRbRlvA=", "narHash": "sha256-rwq5U8dy+a9JFny/73L0SJu1GfWwATMPMTp7D+mjHy8=",
"owner": "nix-community", "owner": "ipetkov",
"repo": "fenix", "repo": "crane",
"rev": "6e6a94c4d0cac4821b6452fbae46609b89a8ddcf", "rev": "8b4f7a4dab2120cf41e7957a28a853f45016bd9d",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nix-community", "owner": "ipetkov",
"repo": "fenix", "repo": "crane",
"type": "github"
}
},
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1673956053,
"narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-parts": {
"inputs": {
"nixpkgs-lib": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1693611461,
"narHash": "sha256-aPODl8vAgGQ0ZYFIRisxYG5MOGSkIczvu2Cd8Gb9+1Y=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "7f53fdb7bdc5bb237da7fefef12d099e4fd611ca",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "flake-parts",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1692799911,
"narHash": "sha256-3eihraek4qL744EvQXsK1Ha6C3CR7nnT8X2qWap4RNk=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "f9e7cf818399d17d347f847525c5a5a8032e4e44",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flake-utils_2": {
"inputs": {
"systems": "systems_2"
},
"locked": {
"lastModified": 1692799911,
"narHash": "sha256-3eihraek4qL744EvQXsK1Ha6C3CR7nnT8X2qWap4RNk=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "f9e7cf818399d17d347f847525c5a5a8032e4e44",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github" "type": "github"
} }
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1686226982, "lastModified": 1693985761,
"narHash": "sha256-nLuiPoeiVfqqzeq9rmXxpybh77VS37dsY/k8N2LoxVg=", "narHash": "sha256-K5b+7j7Tt3+AqbWkcw+wMeqOAWyCD1MH26FPZyWXpdo=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "a64b73e07d4aa65cfcbda29ecf78eaf9e72e44bd", "rev": "0bffda19b8af722f8069d09d8b6a24594c80b352",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -39,24 +113,64 @@
}, },
"root": { "root": {
"inputs": { "inputs": {
"fenix": "fenix", "crane": "crane",
"flake-parts": "flake-parts",
"flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs"
} }
}, },
"rust-analyzer-src": { "rust-overlay": {
"flake": false, "inputs": {
"flake-utils": [
"crane",
"flake-utils"
],
"nixpkgs": [
"crane",
"nixpkgs"
]
},
"locked": { "locked": {
"lastModified": 1686239338, "lastModified": 1693707092,
"narHash": "sha256-c6Mm7UnDf3j3akY3YB3rELFA76QRbB8ttSBsh00LWi0=", "narHash": "sha256-HR1EnynBSPqbt+04/yxxqsG1E3n6uXrOl7SPco/UnYo=",
"owner": "rust-lang", "owner": "oxalica",
"repo": "rust-analyzer", "repo": "rust-overlay",
"rev": "9c03aa1ac2e67051db83a85baf3cfee902e4dd84", "rev": "98ccb73e6eefc481da6039ee57ad8818d1ca8d56",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "rust-lang", "owner": "oxalica",
"ref": "nightly", "repo": "rust-overlay",
"repo": "rust-analyzer", "type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"systems_2": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github" "type": "github"
} }
} }

171
flake.nix
View File

@ -1,103 +1,116 @@
{ {
inputs = { inputs = {
fenix = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
url = "github:nix-community/fenix";
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"; inputs.nixpkgs.follows = "nixpkgs";
}; };
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
}; };
outputs = { self, fenix, nixpkgs }: outputs = inputs@{ flake-parts, flake-utils, crane, nixpkgs, ... }:
let let
inherit (nixpkgs.lib) # Generate the typst package for the given nixpkgs instance.
genAttrs
importTOML
optionals
sourceByRegex
;
eachSystem = f: genAttrs
[
"aarch64-darwin"
"aarch64-linux"
"x86_64-darwin"
"x86_64-linux"
]
(system: f nixpkgs.legacyPackages.${system});
packageFor = pkgs: packageFor = pkgs:
let let
rust = fenix.packages.${pkgs.stdenv.hostPlatform.system}.minimal.toolchain; inherit (nixpkgs.lib)
rustPlatform = pkgs.makeRustPlatform { importTOML
cargo = rust; optionals
rustc = rust; sourceByRegex
}; ;
in Cargo-toml = importTOML ./Cargo.toml;
rustPlatform.buildRustPackage {
pname = "typst";
inherit ((importTOML ./Cargo.toml).workspace.package) version;
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, assets and tests.
src = sourceByRegex ./. [ src = sourceByRegex ./. [
"(assets|crates|tests)(/.*)?" "(assets|crates|tests)(/.*)?"
''Cargo\.(toml|lock)'' ''Cargo\.(toml|lock)''
''build\.rs'' ''build\.rs''
]; ];
cargoLock = { # Typst derivation's args, used within crane's derivation generation
lockFile = ./Cargo.lock; # functions.
allowBuiltinFetchGit = true; commonCraneArgs = {
}; inherit src pname version;
nativeBuildInputs = [ buildInputs = optionals pkgs.stdenv.isDarwin [
pkgs.installShellFiles pkgs.darwin.apple_sdk.frameworks.CoreServices
];
buildInputs = optionals pkgs.stdenv.isDarwin [
pkgs.darwin.apple_sdk.frameworks.CoreServices
];
postInstall = ''
installManPage crates/typst-cli/artifacts/*.1
installShellCompletion \
crates/typst-cli/artifacts/typst.{bash,fish} \
--zsh crates/typst-cli/artifacts/_typst
'';
GEN_ARTIFACTS = "artifacts";
};
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
]; ];
buildInputs = optionals pkgs.stdenv.isDarwin [ nativeBuildInputs = [ pkgs.installShellFiles ];
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;
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
'';
GEN_ARTIFACTS = "artifacts";
});
in
typst;
in
flake-parts.lib.mkFlake { inherit inputs; } {
systems = [
"aarch64-darwin"
"aarch64-linux"
"x86_64-darwin"
"x86_64-linux"
];
flake = {
overlays.default = _: prev: {
typst-dev = packageFor prev;
}; };
});
formatter = eachSystem (pkgs: pkgs.nixpkgs-fmt);
overlays.default = _: prev: {
typst-dev = packageFor prev;
}; };
packages = eachSystem (pkgs: { perSystem = { pkgs, ... }:
default = packageFor pkgs; let
}); inherit (pkgs) lib;
typst = packageFor pkgs;
in
{
packages.default = typst;
apps.default = flake-utils.lib.mkApp {
drv = typst;
};
devShells.default = pkgs.mkShell {
packages = with pkgs; [
rustc
cargo
];
buildInputs = lib.optionals pkgs.stdenv.isDarwin [
pkgs.darwin.apple_sdk.frameworks.CoreServices
pkgs.libiconv
];
};
};
}; };
} }