Skip to content

Instantly share code, notes, and snippets.

@tomberek
Created May 28, 2026 15:04
Show Gist options
  • Select an option

  • Save tomberek/89ea9efcf1e5a3c9d7936d13967265f8 to your computer and use it in GitHub Desktop.

Select an option

Save tomberek/89ea9efcf1e5a3c9d7936d13967265f8 to your computer and use it in GitHub Desktop.
Nar in bash
let
some-derivation = derivation {
name = "hello-world";
system = "x86_64-linux";
builder = "/bin/sh";
args = ["-c" "echo hello world > $out"];
};
a = derivation {
name = "something.nar";
builder = "/bin/sh";
system = "x86_64-linux";
args = ["-c" ''
str(){
len=''${#1}
padlen=$(( (8 - (len % 8)) % 8 ))
int $len
printf "%s" "$1"
pad $padlen
}
int(){
x="$(printf "%016x" "$1")"
rev=
while read -n 2 a; do
rev="$(printf '%s ' "$a" "$rev")"
done <<EOF
$(printf "$x")
EOF
rev="$(printf "\x%s " $rev)"
printf "%b" $rev
}
pad(){
x="$(printf %-''${1}s "")"
printf "''${x// /$(printf "%s" "\x0")}"
}
{
str nix-archive-1
str "("
str "type"
str "directory"
str "entry"
str "("
str "name"
str "some-binary"
str "node"
str "("
str "type"
str "symlink"
str "target"
str "${some-derivation}"
str ")"
str ")"
str "entry"
str "("
str "name"
str "some-binary-2"
str "node"
str "("
str "type"
str "regular"
str "executable" ; str ""
str "contents"
str "some-binary-contents"
str ")"
str ")"
str ")"
} >> $out
''];
};
in
derivation {
name = "unpacker";
builder = "builtin:fetchurl";
outputHashMode = "flat";
system = "no-system";
unpack = true;
url = "file://${a}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment