Install NixOs on an OVH vps with nixos-anywhere

2023 was the year I was nix pilled. 2024 is the year for the experimentations. I discovered the power of NixOs for server deployment with this talk from Carl Dong: The dark arts of NixOs deployments.

He talked about the new nixos-rebuild command to easily change and deploy the configuration of a server and how using kexec(8) and disko to install NixOs on it without the nixos-infect scripts (which are more a hack than a clean way to do it).

This inevitably leads me to nixos-anywhere a great tool using kexec and disko to deploy custom flakes !

The repository gives an example using Hetzner vps provider, but I wanted to try to install nixos on an OVH vps. Here a quick guide of how I did it:

1. Create vps and get credentials

  • Create an ovh account
  • Order vps with debian 12
  • Ovh send to you email with id, ip, and link to create a password

you can connect with ssh debian@<vps-ip>.

2. Change password with passwd and add ssh key to authorized keys.

sudo vim /root/.ssh/authorized_keys
sudo systemctl restart ssh

3. Install nix

sh <(curl -L https://nixos.org/nix/install) --daemon

Nix won’t work in active console sessions until you restart them. exit and reconnect in a new root ssh session.

nix-env -iE "_: with import <nixpkgs/nixos> { configuration = {}; }; \
  with config.system.build; [ nixos-generate-config ]"

nixos-generate-config --no-filesystems --root /mnt

Copy from /mnt the hardware-configuration.nix file.

The rest of the commands should be done on your local machine and not on the target host.

4. Prepare flake

You can find an example of the flake here. Change the hardware-configuration.nix file with the one you copied and change disk-config.nix

5. Test flake

nix run github:nix-community/nixos-anywhere -- --flake .#ovh-vps --vm-test

6. Load nixos-anywhere

nix run github:nix-community/nixos-anywhere -- --flake .#ovh-vps root@<vps-ip>

7. Reload configuration

Add screenfetch to the flake configuration.nix environment.systemPackages

nixos-rebuild switch --flake .#ovh-vps --target-host "root@<vps-ip>"

Connect to the host then check that the screenfetch package was installed.

[root@nixos:~]# screenfetch
          ::::.    ':::::     ::::'         root@nixos
          ':::::    ':::::.  ::::'          OS: NixOS 24.05.20231221.d6863cb
            :::::     '::::.:::::           Kernel: x86_64 Linux 6.1.69
      .......:::::..... ::::::::            Uptime: 7m
     ::::::::::::::::::. ::::::    ::::.    Packages: 470
    ::::::::::::::::::::: :::::.  .::::'    Shell: bash 5.2.21
           .....           ::::' :::::'     Disk: 1.1G / 20G (6%)
          :::::            '::' :::::'      CPU: Intel Core (Haswell, no TSX)
 ........:::::               ' :::::::::::. GPU: Cirrus Logic GD 5446
:::::::::::::                 ::::::::::::: RAM: 250MiB / 1935MiB
 ::::::::::: ..              :::::           
     .::::: .:::            :::::            
    .:::::  :::::          '''''    .....    
    :::::   ':::::.  ......:::::::::::::'    
     :::     ::::::. ':::::::::::::::::'     
            .:::::::: '::::::::::            
           .::::''::::.     '::::.           
          .::::'   ::::.     '::::.          
         .::::      ::::      '::::.         

The Nixos VPS is ready ! Modify and reload configuration as you will ! ❄️