WSL: Ubuntu Nix Flakes Reproducible Environments
Introduction
WARNING
You will need to have followed WSL Ubuntu Nix Setup first post before.
Using Nix flakes you can create declarative and repoducible environments.
Install tools and apps via nix and store them on a repository.
Create Git Repository
First, we need to create a Git Repository.
Create a folder on ~/nix-flakes
mkdir -p ~/nix-flakes
cd nix-flakes
Then intialize a repository
touch README.md
git init
git add .
git commit -m "initial commit"
Create Nix Flakes
We will need to first initialize the Flake
nix flake init
git add flake.nix flake.lock
Add the following to flake.nix
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
home-manager = {
url = "github:nix-community/home-manager";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, home-manager, ... }: let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in {
homeConfigurations.default = home-manager.lib.homeManagerConfiguration {
inherit pkgs;
modules = [ ./home ];
};
};
}
Then create a folder home/ on the Repository
mkdir -p home
And create a file home/default.nix on it with the following content (touch home/default.nix)
{ ... }:
let
username = builtins.getEnv "USER";
in
{
nixpkgs.config.allowUnfree = true;
assertions = [
{
assertion = username != "";
message = "Environment varialbe USER is not set. Set USER";
}
];
home.username = username;
home.homeDirectory = "/home/${username}";
home.stateVersion = "25.11";
imports = [
./core.nix
];
programs.home-manager.enable = true;
}
And add a new file home/core.nix with these packages
{ pkgs, ... }:
{
home.packages = [
pkgs.btop # htop replacement
pkgs.coreutils
pkgs.curl
pkgs.dust # du modern replacement (system storage tool)
pkgs.ffmpeg
pkgs.gnupg
pkgs.libaio
pkgs.libarchive
pkgs.procs # ps replacement
pkgs.sd # sed replacement
pkgs.unixtools.netstat
pkgs.wget
pkgs.wslu
pkgs.xdg-utils
pkgs.xz
pkgs.keychain
pkgs.unzipNLS
pkgs.zip
];
}
Commit the changes
git add .
git commit -m "Configure git flake"
Install Packages
In order to install the package you will need to run from the repo first (cd ~/nix-flakes)
nix run github:nix-community/home-manager#home-manager -- switch --impure --flake .#default
This will install all the packages that you have declared.
Add more Packages
TIP
You can find packages on Nix Packages official website
If you’d like to add more packages you can create a new file (f.e. home/browsers.nix) like
{ pkgs, ... }:
{
home.packages = [
pkgs.firefox
pkgs.browsh
];
}
And add them to imports on home/default.nix
...
imports = [
./core.nix
./browsers.nix
];
...
You will need to commit the changes before applying them
git add .
git commit -m "adding browsers"
Then you can simply run
home-manager switch --impure --flake .#default