Talk:FAQ/Pinning Nixpkgs

From NixOS Wiki
Revision as of 20:04, 5 November 2020 by imported>Expelledboy (Thinking to add a lock file example, but failing)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

I would like to achieve something like a lock file, but optionally used.


# This allows overriding pkgs by passing `--arg production ...`
{
  production ? false,
  nixpkgs ? import <nixpkgs> {}
}:

let
  # Look here for information about how to generate `nixpkgs-version.json`.
  #  → https://nixos.wiki/wiki/FAQ/Pinning_Nixpkgs
  pinned = builtins.fromJSON (builtins.readFile ./nixpkgs.lock);
  inherit (pinned) url rev;
  ref = "nixos-unstable";

  # Load packages from the pinned version.
  pinnedNixpkgs = import (builtins.fetchGit {
    inherit ref url rev;
  }) {};

  # Update ./nixpkgs.lock
  nixpkgsUpdate = nixpkgs.writeScriptBin "update-nixpkgs" ''
    #!/bin/sh
    ${nixpkgs.nix-prefetch-git}/bin/nix-prefetch-git ${url} refs/heads/${ref} > nixpkgs.lock
  '';

  pkgs = if production then pinnedNixpkgs else nixpkgs;
in

with pkgs; mkShell {
  buildInputs = [
    nodejs
    nixpkgsUpdate
  ];
}


Obviously this doens't work, but its close... not sure what I missed