Skip to content

Instantly share code, notes, and snippets.

@LisannaAtHome
Last active December 10, 2017 05:27
Show Gist options
  • Save LisannaAtHome/4b8cd1e64409708f52a1fc1d81e20696 to your computer and use it in GitHub Desktop.
Save LisannaAtHome/4b8cd1e64409708f52a1fc1d81e20696 to your computer and use it in GitHub Desktop.
Hack which installs Nix (single-user) to the root account of a non-NixOS fedora-like Linux distro disk image. Should work with other users, just swap them out. installPkgs.nix takes a disk image with Nix already on it and adds packages to its Nix store and a user account's enviornment.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
{ fetchzip, vmTools, runCommand, utillinux }:
{ name
, srcImg
, nixVersion ? "1.11.15"
, nixSha256 ? "0gr3778zwzay4iwm4s09pc4rgdbg7ykx2g1kskrg790abjw38pa1"
}:
let
src = fetchzip
{ name = "nix-${nixVersion}-x86_64-linux";
url = "https://nixos.org/releases/nix/nix-${nixVersion}/${name}.tar.bz2";
sha256 = nixSha256;
};
postImg = vmTools.runInLinuxVM
(runCommand name
{ buildInputs = [ utillinux ];
preVM =
''mkdir $out
cp --no-preserve=mode ${srcImg} $out/disk.img
diskImage=$out/disk.img
'';
postVM =
''mv $out/disk.img ./disk.img
rm -r $out
mv ./disk.img $out
'';
}
''mountPoint=/mnt
mkdir -v $mountPoint
. /sys/class/block/vda1/uevent
mknod /dev/vda1 b $MAJOR $MINOR
mount /dev/vda1 $mountPoint
cp -r ${src} $mountPoint/nix-binary-dist
mkdir -v $mountPoint/nix
mkdir -v $mountPoint/etc/nix/
echo "build-users-group =" > $mountPoint/etc/nix/nix.conf
export PATH=/usr/bin:$PATH
export USER=root
export HOME=/root
export _NIX_INSTALLER_TEST=foo
chroot $mountPoint /usr/bin/bash /nix-binary-dist/install
rm -r $mountPoint/nix-binary-dist
umount -R $mountPoint
''
);
in postImg
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
{ vmTools, runCommand, utillinux, nixUnstable, perl, file, glibc, lib }:
{ name
, srcImg
, packages
}:
let
inherit (lib) concatStringsSep;
pathList = map (pkg: "${pkg}") packages;
paths = concatStringsSep " " pathList;
postImg = vmTools.runInLinuxVM
(runCommand name
{ buildInputs = [ nixUnstable utillinux perl file glibc ];
memSize = 2048;
exportReferencesGraph = map (x: [("closure-" + baseNameOf x) x]) pathList;
preVM =
''mv closure-* xchg
mkdir $out
cp --no-preserve=mode ${srcImg} $out/disk.img
diskImage=$out/disk.img
'';
postVM =
''mv $out/disk.img ./disk.img
rm -r $out
mv ./disk.img $out
'';
}
''mountPoint=/mnt
mkdir -v $mountPoint
. /sys/class/block/vda1/uevent
mknod /dev/vda1 b $MAJOR $MINOR
mount /dev/vda1 $mountPoint
printRegistration=1 perl ${pathsFromGraph} xchg/closure-* > ./nix-path-registration
nix-store --load-db < ./nix-path-registration
rm ./nix-path-registration
nix-store --verify --check-contents
for path in ${paths}; do
echo "Installing $path"
nix copy --to "local?root=$mountPoint" --no-check-sigs $path
done
chroot $mountPoint /root/.nix-profile/bin/nix-env -i ${paths}
umount -R $mountPoint
''
);
in postImg
@LisannaAtHome
Copy link
Author

You may also need to adjust the memSize of installPkgs.nix, since the nixUnstable nix copy can use a LOT of memory. NixOS/nix#1681

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment