Last active
February 12, 2025 15:57
-
-
Save lukts30/cc207590324149229741f58ae112fedd to your computer and use it in GitHub Desktop.
libguestfs NixOS 24.11 appliance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| pkgs ? import <nixpkgs> { }, | |
| }: | |
| let | |
| stub-pacman = pkgs.writeTextFile { | |
| name = "stub-pacman"; | |
| text = '' | |
| #!/bin/sh | |
| exit 0 | |
| ''; | |
| executable = true; | |
| destination = "/bin/pacman"; | |
| }; | |
| supermin2 = pkgs.supermin.overrideAttrs (previousAttrs: { | |
| patches = [ | |
| # Fixes /lib/modules/ | |
| # https://lists.libguestfs.org/archives/list/[email protected]/thread/XVZXSSFUA5AISDPJKOI35CQB6LFUBXMU/ | |
| (pkgs.fetchpatch { | |
| url = "https://github.com/libguestfs/supermin/commit/8effa5a686037ba3c2f8c97753f398b73cc54881.patch"; | |
| hash = "sha256-urOdAUtjybXrp/6Me/5oHjFTGOxSESplh9E4oJ0c5l4="; | |
| }) | |
| ]; | |
| buildInputs = previousAttrs.buildInputs ++ [ | |
| stub-pacman | |
| pkgs.fakeroot | |
| ]; | |
| }); | |
| etc-arch-release = pkgs.writeTextFile { | |
| name = "arch-release"; | |
| text = ""; | |
| destination = "/etc/arch-release"; | |
| }; | |
| appliancePkgs = with pkgs; [ | |
| libguestfs.guestfsd | |
| bash | |
| coreutils | |
| gnugrep | |
| util-linux | |
| kmod | |
| file | |
| xz | |
| zstd | |
| ntfs3g | |
| fuse3 | |
| fuse | |
| gnutar | |
| gnused | |
| mdadm | |
| findutils | |
| iproute2 | |
| cdrkit | |
| gptfdisk | |
| mtools | |
| parted | |
| gawk | |
| cpio | |
| bzip2 | |
| acl | |
| attr | |
| lvm2 | |
| dosfstools | |
| e2fsprogs | |
| exfatprogs | |
| xfsprogs | |
| btrfs-progs | |
| lzop | |
| procps | |
| lsof | |
| libxml2 | |
| binutils | |
| diffutils | |
| udev | |
| eudev | |
| ]; | |
| myPackages = pkgs.buildEnv { | |
| # extraPrefix = "/usr/"; | |
| name = "my-packages"; | |
| paths = appliancePkgs; | |
| }; | |
| applianceInitSh = pkgs.fetchurl { | |
| url = "https://raw.githubusercontent.com/libguestfs/libguestfs/82e2dea96abe7a90e04dca5de23b91ab8321d6c3/appliance/init"; | |
| sha256 = "sha256-QjB+TRxhL7Aap4fG7wrf8uNZC7Q9TUPRDoeWaW73lsk="; | |
| }; | |
| rootfsLinks = pkgs.runCommand "my-example" { } '' | |
| mkdir build/ | |
| pushd build | |
| ln -s .${myPackages}/bin ./bin | |
| ln -s .${myPackages}/bin ./sbin | |
| cp ${applianceInitSh} ./init | |
| chmod +x ./init | |
| mkdir -p usr/lib64/ | |
| pushd usr/ | |
| ln -s ./lib64 lib | |
| popd | |
| # needs etc... | |
| mkdir dev | |
| mkdir -p var/lib/ | |
| mkdir -p etc/udev/ | |
| # mkdir -p usr/lib64/modules/${pkgs.linux.version}/ | |
| # mkdir -p usr/lib/modules/${pkgs.linux.version}/ | |
| ln -s ../../${pkgs.eudev}/var/lib/udev/rules.d etc/udev/rules.d | |
| popd | |
| mkdir $out | |
| ${pkgs.fakeroot}/bin/fakeroot tar -czvf $out/links.tar.gz -C build/ . | |
| ''; | |
| applianceFHS = ( | |
| pkgs.buildFHSEnv { | |
| name = "pac"; | |
| targetPkgs = pkgs: [ | |
| stub-pacman | |
| etc-arch-release | |
| supermin2 | |
| pkgs.fakeroot | |
| pkgs.cpio | |
| ]; | |
| runScript = pkgs.writeScript "init.sh" '' | |
| mkdir nx2411 | |
| export SUPERMIN_KERNEL=${pkgs.linux}/bzImage | |
| export SUPERMIN_MODULES=${pkgs.linux}/lib/modules/${pkgs.linux.version}/ | |
| export closureInfo=${pkgs.closureInfo { rootPaths = appliancePkgs ++ [ myPackages ]; }} | |
| xargs -I % find % < $closureInfo/store-paths > ./nx2411/hostfiles | |
| ln -f -s ${rootfsLinks}/links.tar.gz ./nx2411/base.tar.gz | |
| ${pkgs.fakeroot}/bin/fakeroot supermin --build --verbose nx2411/ -f ext2 -o ./build/ | |
| ''; | |
| } | |
| ); | |
| appliance = pkgs.stdenv.mkDerivation rec { | |
| name = "appliance"; | |
| version = pkgs.libguestfs-appliance.version; | |
| phases = [ | |
| "buildPhase" | |
| "installPhase" | |
| ]; | |
| buildPhase = '' | |
| ${applianceFHS}/bin/${applianceFHS.name} | |
| ''; | |
| installPhase = '' | |
| mv ./build/ $out | |
| echo "NixOS based appliance" > $out/README.fixed | |
| ''; | |
| }; | |
| in | |
| (pkgs.mkShell { | |
| nativeBuildInputs = [ | |
| appliance | |
| pkgs.e2tools | |
| # pkgs.libguestfs | |
| (pkgs.libguestfs-with-appliance.override { libguestfs-appliance = appliance; }) | |
| ]; | |
| shellHook = '' | |
| export LIBGUESTFS_PATH=${appliance} | |
| # e2ls -l $LIBGUESTFS_PATH/root:/ | |
| # libguestfs-test-tool | |
| ''; | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment