Skip to content

Commit

Permalink
add fallback functionality using latest.json
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
wamserma committed Nov 26, 2023
1 parent 1967cc2 commit ec4969f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
27 changes: 26 additions & 1 deletion latest.json
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
{}
{
"24.05": {
"name": "nixos-24.05pre552472.1b99d72c8b74",
"url": "/nixos/unstable-small/nixos-24.05pre552472.1b99d72c8b74/nixexprs.tar.xz",
"nixexprs_hash": "c05c48bf6c1a02e555ffb8a5703a423665b49e421fdbec57e685addae824e800",
"programs_sqlite_hash": "84721632459c8db1eb38064ccda6f89221543500d26ace5c3f78b582dd8970f5"
},
"22.11": {
"name": "nixos-22.11.4773.ea4c80b39be4",
"url": "/nixos/22.11/nixos-22.11.4773.ea4c80b39be4/nixexprs.tar.xz",
"nixexprs_hash": "c4828378407ebd3255ff9d51d31e942c4a9acd088540c6b83dbd8dc7d24283dd",
"programs_sqlite_hash": "fbb9c05caae92e51e1c4d14018656aeddb2d6222009f6d483dd38ddcbc1f2076"
},
"23.05": {
"name": "nixos-23.05.4970.cbd3f3722ac4",
"url": "/nixos/23.05-small/nixos-23.05.4970.cbd3f3722ac4/nixexprs.tar.xz",
"nixexprs_hash": "75e0ba7717bfab4d6abab3318c4ca8c4c646001f87a2ef6c8e3778a59d96848f",
"programs_sqlite_hash": "4b44b3b01dc11695193a24968d7dc035dba1592ba239c889dc9ad39f2bb0365a"
},
"23.11": {
"name": "nixos-23.11beta470.c9e054502e72",
"url": "/nixos/23.11-small/nixos-23.11beta470.c9e054502e72/nixexprs.tar.xz",
"nixexprs_hash": "866523a1b1ab37ba514320eea08350e92e4434a3f5236ca51b79b7aadc3a3941",
"programs_sqlite_hash": "b6237513b04693c2133675c10feef17a09603f5faae3e0d66047427150b219ee"
}
}
2 changes: 1 addition & 1 deletion programs-sqlite.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ lib, fetchurl, pkgs, rev }:
let
meta = (lib.importJSON ./sources.json)."${rev}";
meta = (lib.importJSON ./sources.json)."${rev}" or (lib.importJSON ./latest.json).${lib.trivial.release};
in
pkgs.stdenvNoCC.mkDerivation {
pname = "programs-sqlite";
Expand Down
8 changes: 7 additions & 1 deletion sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,11 @@
"url": "/nixos/22.11-small/nixos-22.11.1059.ba86945ee00/nixexprs.tar.xz",
"nixexprs_hash": "6bda28ac8d6cc8cf71d444503d3b974c03b5bda1e58a850490188035975e044b",
"programs_sqlite_hash": "d91745ce3aa0512d86c4fdc2dbc14e81227a5fa60c44a021068e77b69653aae5"
},
"53dad94e874c9586e71decf82d972dfb640ef044": {
"name": "nixos-23.05pre470275.53dad94e874",
"url": "/nixos/unstable/nixos-23.05pre470275.53dad94e874/nixexprs.tar.xz",
"nixexprs_hash": "9380965016f278231b12fcc323928ebfb389ce95d816c3e986a597d8883b9aa9",
"programs_sqlite_hash": "9e349c2bde7eb9f751785ed78dfb7723fc54b538ebf4d26286e62a533f577656"
}
}
}
34 changes: 27 additions & 7 deletions test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ let
programs-sqlite-db = flake.packages.${system}.programs-sqlite;
rev = flake.inputs.nixpkgs.rev;

in pkgs.nixosTest {
programs-sqlite-db-for-fallback-test = pkgs.callPackage ./programs-sqlite.nix {
rev = "0000000000000000000000000000000000000000";
};

in
# for the fallback test, we need a rev not in sources.json
assert (false == (pkgs.lib.importJSON ./sources.json) ? revForFallbackTest);

pkgs.nixosTest {
name = "packages-sqlite-test";
nodes = {
directConfig = { config, pkgs, ... }: {
Expand All @@ -43,15 +51,26 @@ in pkgs.nixosTest {
};
};
};

directConfigFallback = { config, pkgs, ... }: {
imports = [ sharedModule ];
users = {
mutableUsers = false;
users = {
root.password = "";
};
};
programs.command-not-found.dbPath = programs-sqlite-db-for-fallback-test;
};
};

testScript = ''
import json;
with open("${flake.outPath}/sources.json") as f:
hashes = json.load(f)
def check(machine, expected_rev, db):
with open(f"${flake.outPath}/{db}", "r") as f:
hashes = json.load(f)
def check(machine):
machine.start()
machine.wait_for_unit("multi-user.target")
Expand All @@ -67,11 +86,12 @@ in pkgs.nixosTest {
cnfsrc = machine.succeed("readlink $(which command-not-found)").strip()
cnfdb = machine.succeed("grep -m 1 dbPath '" + cnfsrc + "' | cut -d '" + '"' + "' -f 2").strip()
cnfdbhash = machine.succeed("sha256sum " + cnfdb + " | cut -d ' ' -f 1").strip()
assert hashes.get("${rev}").get("programs_sqlite_hash") == cnfdbhash, "incorrect programs.sqlite is used"
assert hashes.get(expected_rev).get("programs_sqlite_hash") == cnfdbhash, "incorrect programs.sqlite is used"
machine.shutdown()
check(directConfig)
check(moduleConfig)
check(directConfig, "${rev}", "sources.json")
check(moduleConfig, "${rev}", "sources.json")
check(directConfigFallback, "${pkgs.lib.trivial.release}", "latest.json")
'';
}

0 comments on commit ec4969f

Please sign in to comment.