makeCpioRecursive: init

This commit is contained in:
Graham Christensen 2020-05-25 21:42:02 -04:00
parent ecf42daa66
commit 6ecf5bfa0f
No known key found for this signature in database
GPG Key ID: FE918C3A98C1030F
3 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,27 @@
{ runCommand, nix, jq, path }:
let
map-cpio = ./map-cpio.nix;
makeCpioRecursive = { name, root, prepend ? [], compressor }:
runCommand "${name}-initrd" {
buildInputs = [ nix jq ];
requiredSystemFeatures = [ "recursive-nix" ];
exportReferencesGraph = [ "root" root ];
NIX_PATH = "nixpkgs=${path}";
inherit prepend compressor;
} ''
cat root | grep /nix/store | sort | uniq | jq -R . | jq -s . > paths.json
nix-build ${map-cpio} --arg pathsJson ./paths.json --argstr compressor "$compressor"
touch initrd
for pre in $prepend; do
cat "$pre" >> initrd
done
cat result | xargs cat >> initrd
mkdir $out
mv initrd $out/initrd
'';
in
makeCpioRecursive

View File

@ -0,0 +1,25 @@
{ pathsJson, compressor, pkgs ? import <nixpkgs> {} }:
let
namePart = strPath:
let
last = list: builtins.elemAt list ((builtins.length list) - 1);
stripPathPrefix = path: last (builtins.split "[/]" path);
in
stripPathPrefix "${strPath}";
paths = builtins.fromJSON (builtins.readFile pathsJson);
mkcpio = strPath: pkgs.runCommand "${namePart strPath}-cpio" {
buildInputs = [ pkgs.cpio ];
} ''
mkdir root
cd root
cp -prd --parents ${builtins.storePath strPath} .
find . -print0 | xargs -0r touch -h -d '@1'
find . -print0 \
| sort -z \
| cpio -o -H newc -R +0:+1 --reproducible --null \
| ${compressor} \
> $out
'';
in
pkgs.writeText "cpios" (pkgs.lib.concatMapStringsSep "\n" mkcpio paths)

4
pkgs/default.nix Normal file
View File

@ -0,0 +1,4 @@
{ callPackage }:
{
makeCpioRecursive = callPackage ./cpio-recursive {};
}