chore(lock): move updateFODHash to lock part

This commit is contained in:
phaer 2023-03-15 17:23:18 +01:00 committed by DavHau
parent 530a05eec0
commit 2f162e68e8
3 changed files with 40 additions and 29 deletions

View File

@ -64,6 +64,35 @@
json.dump(lock_data, out_file, indent=2) json.dump(lock_data, out_file, indent=2)
''; '';
updateFODHash = fod: let
unhashedFOD = fod.overrideAttrs (old: {
outputHash = l.fakeSha256;
});
in
config.deps.writePython3 "update-FOD-hash-${config.public.name}" {} ''
import os
import json
import subprocess
out_path = os.getenv("out")
drv_path = "${l.unsafeDiscardStringContext unhashedFOD.drvPath}" # noqa: E501
nix_build = ["${config.deps.nix}/bin/nix", "build", "-L", drv_path] # noqa: E501
with subprocess.Popen(nix_build, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) as process: # noqa: E501
for line in process.stdout:
line = line.strip()
if line == f"error: hash mismatch in fixed-output derivation '{drv_path}':": # noqa: E501
specified = next(process.stdout).strip().split(" ", 1)
got = next(process.stdout).strip().split(" ", 1)
assert specified[0].strip() == "specified:"
assert got[0].strip() == "got:"
hash = got[1].strip()
print(f"Found hash: {hash}")
break
print(line)
with open(out_path, 'w') as f:
json.dump(hash, f, indent=2)
'';
missingError = '' missingError = ''
The lock file ${cfg.lockFileRel} for drv-parts module '${packageName}' is missing, please update it. The lock file ${cfg.lockFileRel} for drv-parts module '${packageName}' is missing, please update it.
To create the lock file, execute:\n ${config.lock.refresh} To create the lock file, execute:\n ${config.lock.refresh}
@ -83,6 +112,8 @@ in {
lock.content = loadedContent; lock.content = loadedContent;
lock.lib = {inherit updateFODHash;};
deps = {nixpkgs, ...}: deps = {nixpkgs, ...}:
l.mapAttrs (_: l.mkDefault) { l.mapAttrs (_: l.mkDefault) {
inherit (nixpkgs) nix; inherit (nixpkgs) nix;

View File

@ -45,5 +45,13 @@ in {
description = "Script to refresh the cache file of this package"; description = "Script to refresh the cache file of this package";
readOnly = true; readOnly = true;
}; };
lib.updateFODHash = l.mkOption {
type = t.functionTo t.path;
description = ''
Helper function to write the hash of a given FOD to $out.
'';
readOnly = true;
};
}; };
} }

View File

@ -11,35 +11,7 @@ in {
../../drv-parts/lock ../../drv-parts/lock
]; ];
lock.fields.mach-nix.pythonSources = let lock.fields.mach-nix.pythonSources = config.lock.lib.updateFODHash config.mach-nix.pythonSources;
safeFOD = config.mach-nix.pythonSources.overrideAttrs (old: {
outputHash = l.fakeSha256;
});
in
config.deps.writePython3 "update-FOD-hash-${config.public.name}" {} ''
import os
import json
import subprocess
out_path = os.getenv("out")
drv_path = "${l.unsafeDiscardStringContext safeFOD.drvPath}" # noqa: E501
nix_build = ["${config.deps.nix}/bin/nix", "build", "-L", drv_path] # noqa: E501
with subprocess.Popen(nix_build, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) as process: # noqa: E501
for line in process.stdout:
line = line.strip()
if line == f"error: hash mismatch in fixed-output derivation '{drv_path}':": # noqa: E501
specified = next(process.stdout).strip().split(" ", 1)
got = next(process.stdout).strip().split(" ", 1)
assert specified[0].strip() == "specified:"
assert got[0].strip() == "got:"
hash = got[1].strip()
print(f"Found hash: {hash}")
break
print(line)
with open(out_path, 'w') as f:
json.dump(hash, f, indent=2)
'';
deps = {nixpkgs, ...}: { deps = {nixpkgs, ...}: {
python = nixpkgs.python39; python = nixpkgs.python39;
inherit (nixpkgs.writers) writePython3; inherit (nixpkgs.writers) writePython3;