Skip to content

Commit 8319500

Browse files
authored
refactor: greatly improves caching of rust packages (#144)
# Problem The problem is broken into two separate parts. The first is that the current Nix structure in the repository, with all packages being placed into the `packages` cell block, does not reflect the actual contents of the files. For example, in many cases, these packages are just Rust libraries that produce zero artifacts during the build process. This makes qualifying them as packages ambiguous at best and potentially harmful at worst. The second relates to the overall repository structure and how it contributes to a problematic packaging experience. The repository is set up as a cargo workspace where individual workspace members share the cargo lockfile located at the workspace root. This is problematic in the Nix ecosystem, which relies on lockfiles for reproducibly building projects. The existing build process (which was copied over before the `std` conversion) relied on building workspace members using this lockfile which inadvertently added a dependency to the whole repository into every single build. In other words, a change in one workspace member would cause **all other workspace members** input hashes to change, thus causing Nix to rebuild them unnecessarily. This is particularly problematic when it comes to having proper caching. In the current state, we effectively cannot reuse cache artifacts across commits due to this unwanted dependency. # Solution The solution to the first stated problem is to refactor packages that are strictly libraries into a separate cell block entitled `libraries`. This at least clearly delineates these as packages that won't produce an artifact (as is the default case with cargo libraries). However, the utility of packaging these workspace members is still questionable. The only real benefit I can foresee is possibly running unit tests during the build process (which we are not currently doing). We should have a follow-up action to discuss if these should just be removed instead to reduce the size of the Nix codebase. The solution to the second problem is a bit more creative. As noted, the general structure of the repository, unfortunately, makes avoiding bringing in unnecessary dependencies into the derivation closure difficult. To get around this, the `mkPackage` function has been reworked to minimize the number of unnecessary inputs being fed into the underlying derivation. This is accomplished by working out the local dependencies of a given package and only including those dependencies in the final closure. This ensures that rebuilds will only happen when the package or one of its dependencies changes. In addition to the above two changes, the substitutes in the `flake.nix` were updated to point to their proper values. This was causing some builds to take over six hours because Nix was trying to "build the world." It's worth nothing that this is primarily due to `voting-tools` and its Haskell dependencies which are known to be unique and thus heavily rely on an existing cache.
2 parents 30efc37 + f956e28 commit 8319500

File tree

18 files changed

+189
-91
lines changed

18 files changed

+189
-91
lines changed

flake.nix

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,40 +36,50 @@
3636
(std.blockTypes.functions "constants")
3737
(std.blockTypes.functions "lib")
3838
(std.blockTypes.functions "toolchains")
39+
(std.blockTypes.installables "artifacts")
40+
(std.blockTypes.installables "libraries")
3941
(std.blockTypes.installables "packages")
4042
(std.blockTypes.nixago "configs")
4143
(std.blockTypes.runnables "operables")
4244
];
4345
}
4446
{
4547
devShells = std.harvest inputs.self ["automation" "devshells"];
48+
artifacts = std.harvest inputs.self [
49+
["artifacts" "artifacts"]
50+
];
51+
containers = std.harvest inputs.self [
52+
["jormungandr" "containers"]
53+
["vit-servicing-station" "containers"]
54+
["vit-testing" "containers"]
55+
];
56+
libraries = std.harvest inputs.self [
57+
["catalyst-toolbox" "libraries"]
58+
["chain-libs" "libraries"]
59+
["chain-wallet-libs" "libraries"]
60+
["jormungandr" "libraries"]
61+
["jortestkit" "libraries"]
62+
["vit-servicing-station" "libraries"]
63+
["vit-testing" "libraries"]
64+
];
4665
packages = std.harvest inputs.self [
47-
["artifacts" "packages"]
4866
["catalyst-toolbox" "packages"]
49-
["chain-libs" "packages"]
50-
["chain-wallet-libs" "packages"]
5167
["jormungandr" "packages"]
52-
["jortestkit" "packages"]
5368
["vit-servicing-station" "packages"]
5469
["vit-testing" "packages"]
5570
["voting-tools" "packages"]
5671
["voting-tools-rs" "packages"]
5772
];
58-
containers = std.harvest inputs.self [
59-
["jormungandr" "containers"]
60-
["vit-servicing-station" "containers"]
61-
["vit-testing" "containers"]
62-
];
6373
};
6474

6575
nixConfig = {
6676
extra-substituters = [
67-
#"https://hydra.iohk.io"
68-
"https://iog-gov-nix.s3.eu-central-1.amazonaws.com"
77+
"https://cache.iog.io"
78+
"https://iog-catalyst-cache.s3.eu-central-1.amazonaws.com"
6979
];
7080
extra-trusted-public-keys = [
7181
"hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ="
72-
"gov:uG8+LG8RqFGScUmOrDkGb4VCbtNhChbnycVnxZxb8AY="
82+
"catalyst:kNW0n7ijUJDvu4BrpqC3j54rgoHNccXx7ABuVzuL9WM="
7383
];
7484
allow-import-from-derivation = "true";
7585
};

nix/catalyst-toolbox/libraries.nix

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
inputs,
3+
cell,
4+
}: let
5+
inherit (inputs) nixpkgs std;
6+
inherit (inputs.cells.lib) lib;
7+
l = nixpkgs.lib // builtins;
8+
9+
name = "catalyst-toolbox";
10+
mkSimplePkg = subPkg: lib.mkPackage {pkgPath = "${name}/${subPkg}";};
11+
in {
12+
snapshot-lib = mkSimplePkg "snapshot-lib";
13+
}

nix/catalyst-toolbox/packages.nix

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77
l = nixpkgs.lib // builtins;
88

99
name = "catalyst-toolbox";
10-
root = inputs.self + "/src/${name}";
11-
12-
mkSimplePkg = subPkg: lib.mkPackage {pkgPath = root + "/${subPkg}";};
10+
mkSimplePkg = subPkg: lib.mkPackage {pkgPath = "${name}/${subPkg}";};
1311
in {
1412
catalyst-toolbox = lib.mkPackage {
15-
pkgPath = root + "/catalyst-toolbox";
13+
pkgPath = "${name}/catalyst-toolbox";
1614
nativeBuildInputs = with nixpkgs; [
1715
postgresql.lib
1816
];
1917
};
20-
snapshot-lib = mkSimplePkg "snapshot-lib";
2118
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
l = nixpkgs.lib // builtins;
88

99
name = "chain-libs";
10-
root = inputs.self + "/src/${name}";
11-
12-
mkSimplePkg = subPkg: lib.mkPackage {pkgPath = root + "/${subPkg}";};
10+
mkSimplePkg = subPkg: lib.mkPackage {pkgPath = "${name}/${subPkg}";};
1311
in {
1412
chain-addr = mkSimplePkg "chain-addr";
1513
cardano-legacy-address = mkSimplePkg "cardano-legacy-address";
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77
l = nixpkgs.lib // builtins;
88

99
name = "chain-wallet-libs";
10-
root = inputs.self + "/src/${name}";
11-
12-
mkSimplePkg = subPkg: lib.mkPackage {pkgPath = root + "/${subPkg}";};
10+
mkSimplePkg = subPkg: lib.mkPackage {pkgPath = "${name}/${subPkg}";};
1311
in {
14-
bip39 = mkSimplePkg "bip39";
1512
chain-path-derivation = mkSimplePkg "chain-path-derivation";
1613
hdkeygen = mkSimplePkg "hdkeygen";
1714
symmetric-cipher = mkSimplePkg "symmetric-cipher";
1815
wallet = mkSimplePkg "wallet";
1916
wallet-c = mkSimplePkg "bindings/wallet-c";
2017
wallet-core = mkSimplePkg "bindings/wallet-core";
21-
wallet-js = mkSimplePkg "bindings/wallet-js";
18+
wallet-wasm-js = mkSimplePkg "bindings/wallet-wasm-js";
2219
}

nix/jormungandr/libraries.nix

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
inputs,
3+
cell,
4+
}: let
5+
inherit (inputs) nixpkgs std;
6+
inherit (inputs.cells.lib) lib;
7+
l = nixpkgs.lib // builtins;
8+
9+
name = "jormungandr";
10+
mkSimplePkg = subPkg: lib.mkPackage {pkgPath = "${name}/${subPkg}";};
11+
in {
12+
blockchain = mkSimplePkg "modules/blockchain";
13+
explorer = mkSimplePkg "explorer";
14+
hersir = mkSimplePkg "testing/hersir";
15+
jormungandr-lib = mkSimplePkg "jormungandr-lib";
16+
loki = mkSimplePkg "testing/loki";
17+
mjolnir = mkSimplePkg "testing/mjolnir";
18+
settings = mkSimplePkg "modules/settings";
19+
thor = mkSimplePkg "testing/thor";
20+
}

nix/jormungandr/operables.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
cell,
44
}: let
55
inherit (inputs) nixpkgs std;
6-
inherit (inputs.cells) artifacts;
6+
inherit (inputs.cells.artifacts) artifacts;
77
inherit (inputs.cells.lib) constants;
88
l = nixpkgs.lib // builtins;
99

1010
mkOperable = package: namespace: let
11-
artifacts' = artifacts.packages."artifacts-${namespace}";
11+
artifacts' = artifacts."artifacts-${namespace}";
1212
in
1313
std.lib.ops.mkOperable {
1414
inherit package;

nix/jormungandr/packages.nix

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,14 @@
77
l = nixpkgs.lib // builtins;
88

99
name = "jormungandr";
10-
root = inputs.self + "/src/${name}";
11-
12-
mkSimplePkg = subPkg: lib.mkPackage {pkgPath = root + "/${subPkg}";};
10+
mkSimplePkg = subPkg: lib.mkPackage {pkgPath = "${name}/${subPkg}";};
1311
in {
1412
jormungandr = lib.mkPackage {
15-
pkgPath = root + "/jormungandr";
13+
pkgPath = "${name}/jormungandr";
1614
cargoOptions = [
1715
"--features"
1816
"prometheus-metrics"
1917
];
2018
};
21-
blockchain = mkSimplePkg "modules/blockchain";
22-
explorer = mkSimplePkg "explorer";
23-
hersir = mkSimplePkg "testing/hersir";
2419
jcli = mkSimplePkg "jcli";
25-
jormungandr-lib = mkSimplePkg "jormungandr-lib";
26-
loki = mkSimplePkg "testing/loki";
27-
mjolnir = mkSimplePkg "testing/mjolnir";
28-
settings = mkSimplePkg "modules/settings";
29-
thor = mkSimplePkg "testing/thor";
3020
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
l = nixpkgs.lib // builtins;
88

99
name = "jortestkit";
10-
root = inputs.self + "/src/${name}";
11-
12-
mkSimplePkg = subPkg: lib.mkPackage {pkgPath = root + "/${subPkg}";};
10+
mkSimplePkg = subPkg: lib.mkPackage {pkgPath = "${name}/${subPkg}";};
1311
in {
1412
jortestkit = mkSimplePkg "";
1513
}

0 commit comments

Comments
 (0)