about summary refs log tree commit diff
path: root/pkgs/sternenseemann
diff options
context:
space:
mode:
authoraszlig <aszlig@nix.build>2021-02-19 10:33:47 +0100
committeraszlig <aszlig@nix.build>2021-02-19 10:33:54 +0100
commit4086bdc8d92b8b0e96546a456c43fb7aab6e81f4 (patch)
tree03fff50afbab1bf44891e05932000740e52d8f62 /pkgs/sternenseemann
parent6603cf3240e9b24245e6d28c2fbd2e566db01ada (diff)
parent4b5fc056a8b7d21faa43d147f20c385468af0398 (diff)
Merge pull request #49 (support programs.sqlite)
This started with pull request #43, which was about disabling the
command-not-found handler. The follow-up pull request, which is what I'm
merging now, however re-uses the programs.sqlite that is shipped as part
of the official nixpkgs channels.

While ideally the programs.sqlite would be in par with the jobset input
for nixpkgsSrc on Hydra, it's not easily possible since we'd need extra
configuration on Hydra, which in turn would make it harder to reproduce
the configuration on other systems. There also is no "tarball" jobset
input, which would at least make it easier to do via declarative
jobsets.

So until we have a definitive solution to this, we currently use
fetchurl to get the programs.sqlite based on a fixed snapshots and in
addition to that, @sternenseeman also wrote a handy updater to make
updating a breeze.

While I didn't test this on one of my machines yet, I did however add a
Hydra jobset for the branch and it so far didn't introduce new eval
errors or build failures, so I consider this safe to merge.

Thanks to @sternenseeman for implementing both #43 and #49.
Diffstat (limited to 'pkgs/sternenseemann')
-rw-r--r--pkgs/sternenseemann/default.nix5
-rw-r--r--pkgs/sternenseemann/vuizvui-update-programs-sqlite/default.nix113
2 files changed, 118 insertions, 0 deletions
diff --git a/pkgs/sternenseemann/default.nix b/pkgs/sternenseemann/default.nix
index c41cb3fa..e0c1bb16 100644
--- a/pkgs/sternenseemann/default.nix
+++ b/pkgs/sternenseemann/default.nix
@@ -116,4 +116,9 @@ lib.fix (self: {
   };
 
   unicode_clock = python3Packages.callPackage ./unicode_clock { };
+
+  vuizvui-update-programs-sqlite = python3Packages.callPackage ./vuizvui-update-programs-sqlite {
+    inherit (pkgs.writers) writePython3;
+    inherit (profpatsch) getBins;
+  };
 })
diff --git a/pkgs/sternenseemann/vuizvui-update-programs-sqlite/default.nix b/pkgs/sternenseemann/vuizvui-update-programs-sqlite/default.nix
new file mode 100644
index 00000000..6e14b948
--- /dev/null
+++ b/pkgs/sternenseemann/vuizvui-update-programs-sqlite/default.nix
@@ -0,0 +1,113 @@
+{ writePython3
+, getBins
+, requests
+, nix
+, gnutar
+}:
+
+let
+
+  bins = (getBins nix [ "nix-hash" ])
+      // (getBins gnutar [ "tar" ]);
+
+in
+
+writePython3 "vuizvui-update-programs-sqlite" {
+  flakeIgnore = [
+    # whitespaces around { }
+    "E201" "E202"
+    # fuck 4-space indentation
+    "E121" "E111"
+    # who cares about blank lines
+    "W391" "E302" "E305"
+    # URLs are long
+    "E501"
+  ];
+  libraries = [ requests ];
+} ''
+  from pathlib import Path
+  import re
+  import requests
+  import subprocess
+  import sys
+  from tempfile import TemporaryDirectory
+
+  def latest_nixexprs_url():
+    r = requests.head('https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz')
+
+    assert r.status_code == 301
+    return r.headers['location']
+
+  def nixos_version_for_url(url):
+    match = re.match(r"https://releases\.nixos\.org/nixos/unstable/nixos-([0-9a-fpre.]+)/nixexprs\.tar\.xz", url)
+    return match.group(1)
+
+  def download(url: str, filename: Path) -> Path:
+    with requests.get(url, stream=True) as r:
+      r.raise_for_status()
+      with open(filename, 'wb') as f:
+        for c in r.iter_content(chunk_size=16384):
+          f.write(c)
+
+    return filename
+
+  def main():
+    if len(sys.argv) > 2:
+        print(f'Usage: {sys.argv[0]} /path/to/release.nix', file=sys.stderr)
+        raise SystemExit(64)
+
+    url = latest_nixexprs_url()
+    version = nixos_version_for_url(url)
+
+    print(f'Updating programs.sqlite to {version}', file=sys.stderr)
+
+    with TemporaryDirectory(prefix="vuizvui-update-programs-sqlite") as dir:
+      nixexprs = download(url, dir / Path('nixexprs.tar.xz'))
+      programs_sqlite = dir / Path('programs.sqlite')
+
+      with open(programs_sqlite, 'wb') as f:
+        subprocess.run([
+            '${bins.tar}',
+            '-xJOf',
+            nixexprs,
+            f'nixos-{version}/programs.sqlite'
+          ], stdout=f, check=True)
+
+      hash = subprocess.run([
+          '${bins.nix-hash}',
+          '--base32',
+          '--type', 'sha256',
+          '--flat',
+          programs_sqlite
+        ],
+        check=True,
+        capture_output=True).stdout.decode('utf-8').strip()
+
+      print(f'New hash: {hash}', file=sys.stderr)
+
+      if len(sys.argv) == 1:
+        print('Doing nothing (dry run)', file=sys.stderr)
+      elif len(sys.argv) == 2:
+        release_nix = Path(sys.argv[1])
+
+        with open(release_nix, 'r+') as f:
+          text = f.read()
+          # base32 alphabet as per nix-rust/src/util/base32.rs
+          new_text = re.sub(r'programsSqliteSha256\s*=\s*"[0-9a-fg-np-sv-z]+"',
+                            f'programsSqliteSha256 = "{hash}"',
+                            text)
+          new_text = re.sub(r'programsSqliteVersion\s*=\s*"[0-9a-fpre.]+"',
+                            f'programsSqliteVersion = "{version}"',
+                            new_text)
+
+          if text == new_text:
+            print('Already up to date')
+          else:
+            f.seek(0)
+            f.write(new_text)
+
+            print(f'Wrote to {release_nix}', file=sys.stderr)
+
+  if __name__ == '__main__':
+    main()
+  ''