How to Install Nix on any Linux VPS

Nix is a versatile package manager, programming language, and even a complete Linux distribution. It stands out with its focus on reproducibility and declarative configuration.

Download and Run the Installer

We'll be using determinate system's nix installer

curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- --install
Screenshot 2024-06-26 at 12 53 49

You should have nix installed by now.

nix --version

Screenshot 2024-06-26 at 12 57 01

Let's install a pkg tmux

nix profile install nixpkgs#tmux

Screenshot 2024-06-26 at 12 58 46

we can even install package for temp shell

nix-shell -p git

Screenshot 2024-06-26 at 13 08 29 Screenshot 2024-06-26 at 13 08 37

Installing Home Manager

Finally, we'll use Home Manager, a tool designed to manage the user environment (e.g., dotfiles) in a declarative way using Nix. We'll start by adding the minimal configuration needed to get Home Manager up and running.

First, create a dotfiles directory if you don't have one already (the name of the directory does not matter). Use git init (or your favorite GUI) to initialize a Git repository in the folder. Add the following two files:

mkdir dotfiles; cd dotfiles; git init

Screenshot 2024-06-26 at 13 13 40
{
  description = "Your description here";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    home-manager = {
      url = "github:nix-community/home-manager";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { nixpkgs, home-manager, ... }: {
    homeConfigurations = {
      <Yourname> = home-manager.lib.homeManagerConfiguration {
        pkgs = nixpkgs.legacyPackages.x86_64-linux;
        modules = [
          ./modules
          {
            home.username = "username";
            home.stateVersion = "23.11";
            home.homeDirectory = "/home/username";
          }
        ];
      };

    };
  };
}

Place the above file as flake.nix.

Now the directory tree should look like this

Screenshot 2024-06-26 at 13 18 31

This flake initiales all the modules from modules directory.

For example to configure git, it goes something like this

{ pkgs, ... }:
let
  name = "username";
  email = "main";
in
{
  programs.git = {
    enable = true;
    userName = name;
    userEmail = email;
  };
}

Now we'll need to source it inside modules,

Like lua's init.lua' we have default.nix in nix.

We'll now import out git.nix using modules/default.nix file and it goes something like this.

{ config, ... }:

let
  modules = [
    ./git.nix
  ];
in
{
  imports = modules;

  xdg.dataHome = "${config.home.homeDirectory}/.local/share";
  programs.home-manager.enable = true;
}

We can now build our config using following.

nix run github:nix-community/home-manager -- switch --flake .#your_name
Screenshot 2024-06-26 at 13 24 51

It should generate your config like below.

Screenshot 2024-06-26 at 13 26 58