From dfb73342a094871a54185d90841430be087e109c Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Fri, 29 Nov 2024 08:01:14 +0100 Subject: refactor: switch to npins niv is written in Haskell, while npins is written in Rust. I'm more comfortable with Rust. npins also seems more actively maintained. The files in npins/ were generated with: nix-shell -p npins npins init --bare npins add --name nixos channel nixos-24.05 --- nixos/README.md | 14 ++++----- nixos/nixpkgs/path.nix | 2 +- nixos/npins/default.nix | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ nixos/npins/sources.json | 11 +++++++ nixos/sanix.nix | 4 +-- 5 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 nixos/npins/default.nix create mode 100644 nixos/npins/sources.json (limited to 'nixos') diff --git a/nixos/README.md b/nixos/README.md index e96348b..6c4bcb1 100644 --- a/nixos/README.md +++ b/nixos/README.md @@ -4,11 +4,11 @@ This is a fairly straightforward setup for making a NixOS system configuration s This means: - `nix-channel` is disabled -- Nixpkgs is managed with [niv](https://github.com/nmattia/niv) [^1] +- Nixpkgs is managed with [npins](https://github.com/andir/npins) [^1] - The same Nixpkgs is used for the system and all Nix commands - This includes the Nixpkgs version, config and overlays -[^1]: Yes niv is a third-party tool, but it's essentially just a nice wrapper around `nix-prefetch-url` and co. +[^1]: Yes npins is a third-party tool, but it's essentially just a nice wrapper around `nix-prefetch-url` and co. ## Usage @@ -31,10 +31,10 @@ We're assuming that you just installed NixOS by going through the [official inst ``` nixos-generate-config --dir . ``` -3. Pin Nixpkgs to the [latest stable version](https://nixos.org/manual/nixos/stable/release-notes) using [niv](https://github.com/nmattia/niv): +3. Pin Nixpkgs to the [latest stable version](https://nixos.org/manual/nixos/stable/release-notes) using npins: ``` - nix-shell -p niv --run \ - 'niv init --nixpkgs NixOS/nixpkgs --nixpkgs-branch nixos-23.11' + nix-shell -p npins --run \ + 'npins init --bare && npins add --name nixos channel nixos-23.11' ``` 4. Remove all stateful channels: ``` @@ -52,11 +52,11 @@ Here are some changes you can make: - Change the NixOS configuration in `./configuration.nix` - Update the pinned Nixpkgs: ``` - niv update nixpkgs + npins update nixos ``` - Upgrade to a newer release: ``` - niv update nixpkgs --branch nixos-23.11 + npins add --name nixos --channel nixos-23.11 ``` - Change the Nixpkgs config by editing `nixpkgs/config.nix` - Add Nixpkgs overlays to `nixpkgs/overlays.nix` diff --git a/nixos/nixpkgs/path.nix b/nixos/nixpkgs/path.nix index ea4a74c..7ae177e 100644 --- a/nixos/nixpkgs/path.nix +++ b/nixos/nixpkgs/path.nix @@ -1,2 +1,2 @@ # The Nixpkgs path to use -(import ../nix/sources.nix).nixpkgs.outPath +(import ../npins).nixos.outPath diff --git a/nixos/npins/default.nix b/nixos/npins/default.nix new file mode 100644 index 0000000..5e7d086 --- /dev/null +++ b/nixos/npins/default.nix @@ -0,0 +1,80 @@ +# Generated by npins. Do not modify; will be overwritten regularly +let + data = builtins.fromJSON (builtins.readFile ./sources.json); + version = data.version; + + mkSource = + spec: + assert spec ? type; + let + path = + if spec.type == "Git" then + mkGitSource spec + else if spec.type == "GitRelease" then + mkGitSource spec + else if spec.type == "PyPi" then + mkPyPiSource spec + else if spec.type == "Channel" then + mkChannelSource spec + else + builtins.throw "Unknown source type ${spec.type}"; + in + spec // { outPath = path; }; + + mkGitSource = + { + repository, + revision, + url ? null, + hash, + branch ? null, + ... + }: + assert repository ? type; + # At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository + # In the latter case, there we will always be an url to the tarball + if url != null then + (builtins.fetchTarball { + inherit url; + sha256 = hash; # FIXME: check nix version & use SRI hashes + }) + else + assert repository.type == "Git"; + let + urlToName = + url: rev: + let + matched = builtins.match "^.*/([^/]*)(\\.git)?$" repository.url; + + short = builtins.substring 0 7 rev; + + appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else ""; + in + "${if matched == null then "source" else builtins.head matched}${appendShort}"; + name = urlToName repository.url revision; + in + builtins.fetchGit { + url = repository.url; + rev = revision; + inherit name; + # hash = hash; + }; + + mkPyPiSource = + { url, hash, ... }: + builtins.fetchurl { + inherit url; + sha256 = hash; + }; + + mkChannelSource = + { url, hash, ... }: + builtins.fetchTarball { + inherit url; + sha256 = hash; + }; +in +if version == 3 then + builtins.mapAttrs (_: mkSource) data.pins +else + throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`" diff --git a/nixos/npins/sources.json b/nixos/npins/sources.json new file mode 100644 index 0000000..2deef59 --- /dev/null +++ b/nixos/npins/sources.json @@ -0,0 +1,11 @@ +{ + "pins": { + "nixos": { + "type": "Channel", + "name": "nixos-24.05", + "url": "https://releases.nixos.org/nixos/24.05/nixos-24.05.6668.e8c38b73aeb2/nixexprs.tar.xz", + "hash": "0lhh36z3fvd3b64dz7an08y3c3shb67aj17ny9z28bs21i3dc5yh" + } + }, + "version": 3 +} \ No newline at end of file diff --git a/nixos/sanix.nix b/nixos/sanix.nix index 4895127..02d9ed5 100644 --- a/nixos/sanix.nix +++ b/nixos/sanix.nix @@ -1,7 +1,7 @@ { lib, pkgs, config, ... }: { environment.systemPackages = with pkgs; [ - # We're using niv to manage the systems Nixpkgs version, install it globally for ease - niv + # We're using npins to manage the systems Nixpkgs version, install it globally for ease + npins ]; # Use the Nixpkgs config and overlays from the local files for this NixOS build -- cgit v1.2.3