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
You should have nix installed by now.
nix --version
Let's install a pkg tmux
nix profile install nixpkgs#tmux
we can even install package for temp shell
nix-shell -p git
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
{
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
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
It should generate your config like below.