|
| 1 | +--- |
| 2 | +title: Synchronize flake inputs |
| 3 | +description: How to not end up with 20 different compiler versions |
| 4 | +date: Jun 22, 2022 |
| 5 | +tags: [ nix, flakes ] |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +# Synchronize flake inputs |
| 10 | + |
| 11 | +When working across multiple rust crates, of course as nix flakes, |
| 12 | +it is very easy to end up with multiple minor versions of the toolchain. |
| 13 | +Which is annoying and does not help all that much at all - |
| 14 | +the strong nix reproducibility is a nuisance in this case. |
| 15 | + |
| 16 | +## Solution |
| 17 | + |
| 18 | +Using a super-flake to re-export the packages we can get the same version |
| 19 | +of the toolchain for multiple crate flakes. |
| 20 | + |
| 21 | +The super-flake will look something like this: |
| 22 | + |
| 23 | +```nix |
| 24 | +{ |
| 25 | + inputs = { |
| 26 | + flake-utils.url = github:numtide/flake-utils/master; |
| 27 | + nixpkgs.url = github:NixOS/nixpkgs/nixos-22.05; |
| 28 | + rust-overlay = { |
| 29 | + url = github:oxalica/rust-overlay/master; |
| 30 | + inputs = { |
| 31 | + nixpkgs.follows = "nixpkgs"; |
| 32 | + flake-utils.follows = "flake-utils"; |
| 33 | + }; |
| 34 | + }; |
| 35 | + }; |
| 36 | +
|
| 37 | + outputs = { self, flake-utils, nixpkgs, rust-overlay }: |
| 38 | + { |
| 39 | + lib = flake-utils.lib; |
| 40 | + } |
| 41 | + // |
| 42 | + flake-utils.lib.eachDefaultSystem (system: rec |
| 43 | + { |
| 44 | + packages = (import nixpkgs { |
| 45 | + inherit system; |
| 46 | + overlays = [ |
| 47 | + rust-overlay.overlay |
| 48 | + ]; |
| 49 | + }); |
| 50 | + }); |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +We re-export the `nixpkgs` with `rust-overlay` applied, |
| 55 | +and also `flake-utils.lib` for convenience. |
| 56 | + |
| 57 | +The usage is simple: |
| 58 | + |
| 59 | +```nix |
| 60 | +{ |
| 61 | + inputs = { |
| 62 | + nix.url = github:${REPO_OWNER}/${REPO_NAME}/master; |
| 63 | + }; |
| 64 | +
|
| 65 | + outputs = { self, nix }: |
| 66 | + with nix.lib; |
| 67 | + eachSystem [ system.x86_64-linux ] (system: { |
| 68 | + devShell = import ./shell.nix { |
| 69 | + pkgs = nix.packages.${system}; |
| 70 | + }; |
| 71 | + }); |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +## Bonus: auto-update |
| 76 | + |
| 77 | +To auto update the input we can add the following command to the end of |
| 78 | +[.envrc](https://direnv.net/): |
| 79 | + |
| 80 | +```sh |
| 81 | +nix flake update nix --commit-lock-file |
| 82 | +``` |
0 commit comments