From e74640cf16fecfdc00b0ec0c90a33ae0bd4b1c45 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Wed, 5 Jun 2024 21:16:01 +0800 Subject: [PATCH] bahaghari: use npins for pinning nixpkgs branches This makes it possible for easier way to contribute with non-flakes usage. --- subprojects/bahaghari/flake.nix | 9 +-- subprojects/bahaghari/npins/default.nix | 80 ++++++++++++++++++++++++ subprojects/bahaghari/npins/sources.json | 29 +++++++++ subprojects/bahaghari/shell.nix | 16 ++++- subprojects/bahaghari/tests/default.nix | 7 ++- 5 files changed, 133 insertions(+), 8 deletions(-) create mode 100644 subprojects/bahaghari/npins/default.nix create mode 100644 subprojects/bahaghari/npins/sources.json diff --git a/subprojects/bahaghari/flake.nix b/subprojects/bahaghari/flake.nix index 6c38dd49..2e3c710a 100644 --- a/subprojects/bahaghari/flake.nix +++ b/subprojects/bahaghari/flake.nix @@ -2,15 +2,16 @@ description = "Specialized set of Nix modules for generating and applying themes."; inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; flake-utils.url = "github:numtide/flake-utils"; }; - outputs = inputs@{ self, nixpkgs, ... }: - let systems = inputs.flake-utils.lib.defaultSystems; + outputs = inputs@{ self, ... }: + let + systems = inputs.flake-utils.lib.defaultSystems; + sources = import ./npins; in inputs.flake-utils.lib.eachSystem systems (system: { devShells.default = - import ./shell.nix { pkgs = import nixpkgs { inherit system; }; }; + import ./shell.nix { pkgs = import sources.nixos-stable { inherit system; }; }; }) // import ./default.nix { }; } diff --git a/subprojects/bahaghari/npins/default.nix b/subprojects/bahaghari/npins/default.nix new file mode 100644 index 00000000..5e7d086e --- /dev/null +++ b/subprojects/bahaghari/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/subprojects/bahaghari/npins/sources.json b/subprojects/bahaghari/npins/sources.json new file mode 100644 index 00000000..d26ca022 --- /dev/null +++ b/subprojects/bahaghari/npins/sources.json @@ -0,0 +1,29 @@ +{ + "pins": { + "nixos-stable": { + "type": "Git", + "repository": { + "type": "GitHub", + "owner": "NixOS", + "repo": "nixpkgs" + }, + "branch": "nixos-24.05", + "revision": "b3b2b28c1daa04fe2ae47c21bb76fd226eac4ca1", + "url": "https://github.com/NixOS/nixpkgs/archive/b3b2b28c1daa04fe2ae47c21bb76fd226eac4ca1.tar.gz", + "hash": "06jxcg73ymibvfg3czmq856f84bkhxhqgwa51mf87xprjz74zxks" + }, + "nixos-unstable": { + "type": "Git", + "repository": { + "type": "GitHub", + "owner": "NixOS", + "repo": "nixpkgs" + }, + "branch": "nixos-unstable", + "revision": "57610d2f8f0937f39dbd72251e9614b1561942d8", + "url": "https://github.com/NixOS/nixpkgs/archive/57610d2f8f0937f39dbd72251e9614b1561942d8.tar.gz", + "hash": "0k8az8vmfdk1n8xlza252sqk0hm1hfc7g67adin6jxqaab2s34n9" + } + }, + "version": 3 +} diff --git a/subprojects/bahaghari/shell.nix b/subprojects/bahaghari/shell.nix index b3b8623b..b21dfbd4 100644 --- a/subprojects/bahaghari/shell.nix +++ b/subprojects/bahaghari/shell.nix @@ -1,7 +1,19 @@ -{ pkgs ? import { } }: +let + sources = import ./npins; +in +{ pkgs ? import sources.nixos-stable { } }: with pkgs; mkShell { - inputsFrom = [ nix ]; + inputsFrom = [ + nix + ]; + + packages = [ + npins + + treefmt + nixpkgs-fmt + ]; } diff --git a/subprojects/bahaghari/tests/default.nix b/subprojects/bahaghari/tests/default.nix index a151c1a9..0b5474ed 100644 --- a/subprojects/bahaghari/tests/default.nix +++ b/subprojects/bahaghari/tests/default.nix @@ -1,7 +1,10 @@ # This is the unit cases for our Nix project. -{ pkgs ? import { } }: - +{ branch ? "nixos-stable" }: +let + sources = import ../npins; + pkgs = import sources.${branch} { }; +in { lib = import ./lib { inherit pkgs; }; #modules = import ./modules { inherit pkgs; };