Storage optimization: Difference between revisions

imported>Ianthehenry
larger tweaks to the section on pinning
imported>Pinage404
Line 144: Line 144:
This is easiest to set up while installing NixOS, but <code>/nix</code> can be moved on a live system:
This is easiest to set up while installing NixOS, but <code>/nix</code> can be moved on a live system:


# Create a new partition and mount it over <code>/mnt</code>
''All commands below are executed with root privileges''
# <code>rsync -aAxv</code> everything from <code>/nix</code> to <code>/mnt</code>
# Bind <code>/mnt</code> to <code>/nix</code> (e.g. using <code>mount</code>).
# Restart nix daemon with <code>systemctl restart nix-daemon.service </code> <code>systemctl restart nix-daemon.socket </code>.
# Rerun <code>nixos-rebuild switch</code> with something like <syntaxhighlight lang="bash">fileSystems."/nix" = { device = "/dev/disk/by-label/nix";  neededForBoot = true; options = [ "noatime" ]; };</syntaxhighlight>


# Reboot to be sure <code>/nix/store</code> is properly mounted.
# Create a new partition
# Once you are sure everything works, you can delete the old store doing a bind mount <code>/</code> to <code>/old_root</code>, and remove <code>/old_root/nix</code>.
# Mount this new partition over <code>/mnt</code> <syntaxhighlight lang="bash">
mount /dev/disk/by-label/nix /mnt
</syntaxhighlight>
# Copy everything from <code>/nix</code> to <code>/mnt</code> ''Trailing slash are important'' <syntaxhighlight lang="bash">
rsync --archive --acls --one-file-system --verbose /nix/store/ /mnt/store
rsync --archive --acls --one-file-system --verbose /nix/var/ /mnt/var
</syntaxhighlight>
# Use the new partition as new <code>/nix</code> <syntaxhighlight lang="bash">
umount /mnt
mount /dev/disk/by-label/nix /nix
</syntaxhighlight>
# Restart nix daemon <syntaxhighlight lang="bash">
systemctl stop nix-daemon.service
systemctl restart nix-daemon.socket
systemctl start nix-daemon.service
</syntaxhighlight>
# Add mounting the <code>/nix</code> partition to your <code>/etc/nixos/configuration.nix</code> <syntaxhighlight lang="nix">
{
  # ...
  fileSystems."/nix" = {
    device = "/dev/disk/by-label/nix";
    fsType = "ext4";
    neededForBoot = true;
    options = [ "noatime" ];
  };
  # ...
}
</syntaxhighlight>
# Apply your configuration <syntaxhighlight lang="bash">
nixos-rebuild switch
</syntaxhighlight>
# Reboot to be sure <code>/nix/store</code> is properly mounted
# After reboot, check that <code>/nix</code> is mounted over your partition <syntaxhighlight lang="bash">
mount | grep "/nix" && echo "Nix seems to use your new partition" || echo "It seems that something bad happened"
</syntaxhighlight>
# Once '''you are sure''' everything works, you can delete the old store <syntaxhighlight lang="bash">
mkdir /tmp/old_root
mount --bind / /tmp/old_root
rm --recursive /tmp/old_root/nix
umount /tmp/old_root
rmdir /tmp/old_root
</syntaxhighlight>


Keep in mind that all commands like <code>mount</code> and <code>bash</code> point to some executable in <code>/nix/store</code>, so never mount an empty disk over <code>/nix</code> or <code>/nix/store</code>, otherwise you will be locked out until reboot!
Keep in mind that all commands like <code>mount</code> and <code>bash</code> point to some executable in <code>/nix/store</code>, so never mount an empty disk over <code>/nix</code> or <code>/nix/store</code>, otherwise you will be locked out until reboot!