Difference between revisions of "Move Nix Store to new partition"

From NixOS Wiki
Jump to: navigation, search
m (Formatting fixes)
Line 1: Line 1:
The tutorial will teach you how to relocate `/nix` to a new
+
The tutorial will teach you how to relocate {{ic|/nix}} to a new
device on non `NixOS` Hosts. A summery of how this could be done on nixOS is below
+
device on non NixOS Hosts. A summery of how this could be done on NixOS is below
 
'''This has not been tested on NixOS'''  but there may still be a way:
 
'''This has not been tested on NixOS'''  but there may still be a way:
  
Line 9: Line 9:
 
The device that /nix is on has no more space
 
The device that /nix is on has no more space
  
    $ du -h
+
<syntaxhighlight lang="console">
    Filesystem      Size  Used Avail Use% Mounted on
+
$ du -h
    /dev/sda1      182G  180G    2G  99% /
+
Filesystem      Size  Used Avail Use% Mounted on
 
+
/dev/sda1      182G  180G    2G  99% /
 +
</syntaxhighlight>
 
Stop nix daemon
 
Stop nix daemon
  
    $ sudo systemctl stop nix-daemon.service
+
<syntaxhighlight lang="console">
 +
$ sudo systemctl stop nix-daemon.service
 +
</syntaxhighlight>
 +
Move backup /nix; mv /nix to /nix.bak
  
Move backup /nix; mv /nix to /nix.bak
+
<syntaxhighlight lang="console">
 +
$ ls -alF /nix
 +
total 1068
 +
drwxr-xr-x    4 root root      4096 Jan 22 02:43 ./
 +
drwxr-xr-x  26 root root      4096 Feb  8 12:17 ../
 +
drwxrwxr-t 1555 root nixbld 1077248 Feb  8 12:05 store/
 +
drwxr-xr-x    4 root root      4096 Jan 22 02:43 var/
 +
</syntaxhighlight>
  
    $ ls -alF /nix
+
<syntaxhighlight lang="console">
    total 1068
+
$ cd /
    drwxr-xr-x    4 root root      4096 Jan 22 02:43 ./
+
$ tar -afp /nix.tgz /nix /nix.bak
    drwxr-xr-x  26 root root      4096 Feb  8 12:17 ../
+
$ sudo mv /nix /nix.bak # in case something goes horribly wrong
    drwxrwxr-t 1555 root nixbld 1077248 Feb  8 12:05 store/
+
</syntaxhighlight>
    drwxr-xr-x    4 root root      4096 Jan 22 02:43 var/
 
    $ cd /
 
    $ tar -afp /nix.tgz /nix /nix.bak
 
    $ sudo mv /nix /nix.bak # incase something goes horribly wrong
 
  
 
Mount new device. You will need to add this device to `/etc/fstab`
 
Mount new device. You will need to add this device to `/etc/fstab`
  
    $ mount /nix
+
<syntaxhighlight lang="console">
    $ du -h
+
$ mount /nix
    Filesystem      Size  Used Avail Use% Mounted on
+
$ du -h
    /dev/sdb1      300G    0G  300G  0% /nix
+
Filesystem      Size  Used Avail Use% Mounted on
 
+
/dev/sdb1      300G    0G  300G  0% /nix
 +
</syntaxhighlight>
 
Restore /nix from /nix.tgz; verify contents
 
Restore /nix from /nix.tgz; verify contents
  
    $ cd /
+
<syntaxhighlight lang="console">
    $ sudo tar -xf /nix.tgz  
+
$ cd /
    $ nix-store --verify --check-contents
+
$ sudo tar -xf /nix.tgz  
 
+
$ nix-store --verify --check-contents
Start up nixdaemon
+
</syntaxhighlight>
  
    $ sudo systemctl start nix-daemon.service
+
Start {{ic|nix-daemon}}
  
 +
<syntaxhighlight lang="console">
 +
$ sudo systemctl start nix-daemon.service
 +
</syntaxhighlight>
 
= NixOS =
 
= NixOS =
  
Line 54: Line 65:
 
run the installer from grub at any time.
 
run the installer from grub at any time.
  
adjust your configuration.nix so it expects /nix to be at the new place, and nixos-rebuild boot, so it doesnt break  after you move it
+
adjust your configuration.nix so it expects {{ic|/nix}} to be at the new place, and nixos-rebuild boot, so it doesnt break  after you move it
  
 
Now reboot and move it.
 
Now reboot and move it.

Revision as of 08:58, 3 June 2021

The tutorial will teach you how to relocate /nix to a new device on non NixOS Hosts. A summery of how this could be done on NixOS is below This has not been tested on NixOS but there may still be a way:


On *BSD/Linux Host

The device that /nix is on has no more space

$ du -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       182G  180G    2G  99% /

Stop nix daemon

$ sudo systemctl stop nix-daemon.service

Move backup /nix; mv /nix to /nix.bak

$ ls -alF /nix
total 1068
drwxr-xr-x    4 root root      4096 Jan 22 02:43 ./
drwxr-xr-x   26 root root      4096 Feb  8 12:17 ../
drwxrwxr-t 1555 root nixbld 1077248 Feb  8 12:05 store/
drwxr-xr-x    4 root root      4096 Jan 22 02:43 var/
$ cd /
$ tar -afp /nix.tgz /nix /nix.bak
$ sudo mv /nix /nix.bak # in case something goes horribly wrong

Mount new device. You will need to add this device to `/etc/fstab`

$ mount /nix
$ du -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       300G    0G  300G   0% /nix

Restore /nix from /nix.tgz; verify contents

$ cd /
$ sudo tar -xf /nix.tgz 
$ nix-store --verify --check-contents

Start nix-daemon

$ sudo systemctl start nix-daemon.service

NixOS

Add rescue-boot.nix to your imports section.

nixos-rebuild (and pray you have ~300mb free on /boot/)

run the installer from grub at any time.

adjust your configuration.nix so it expects /nix to be at the new place, and nixos-rebuild boot, so it doesnt break after you move it

Now reboot and move it.