OBSOLETE: Tutorial how to start using NixOS on Windows with WSL
version: 1.0.0 date: 2024-09-15 author: bestia.dev repository: GitHub
It is too complicated and I don't want to learn yet another obscure language. I will still use bash scripts, podman and buildah to create Debian container images for Linux instead.
It sounds like NixOS is an interesting Linux OS for easy declarative management to recreate the same OS many times. The main point is reproducibility.
Then the Nix package manager is very interesting for ephemeral installations of programs that are easily removed. Good for experimenting and testing.
Let's try it out!
https://nix-community.github.io/NixOS-WSL/install.html
- download the 0,5GB tar
Run in git-bash on windows:
wsl --import NixOS --version 2 $USERPROFILE/NixOS/ nixos-wsl.tar.gz
# enter NixOS shell:
wsl -d NixOS
Run in NixOS shell:
# update Nix channel:
sudo nix-channel --update
# after a declarative change rebuild the OS:
sudo nixos-rebuild switch
#check version:
nixos-version
I don't want NixOS (guest) to see the filesystem on windows (host).
I need to make changes to NixOS /etc/wsl.conf
, but this file is readonly. I have to tinker with /etc/nixos/configuration.nix
and then rebuild the system. This is the NixOS way of doing things. Makes sense.
sudo nano /etc/nixos/configuration.nix
Here I added these lines:
wsl.enable = true;
wsl.defaultUser = "nixos";
# disable auto-mounting of win drives
wsl.wslConf.automount.enabled = false;
# process fstab entries to allow some folders to be mounted
wsl.wslConf.automount.mountFsTab = true;
# disable launching windows exe files
wsl.wslConf.interop.enabled = false;
# disable appending thw windows path
wsl.wslConf.interop.appendWindowsPath = false;
Finally rebuild the system and switch:
sudo nixos-rebuild switch
# now the wsl.conf is changed, but not yet used
# exit from WSL
exit
Shutdown wsl from git-bash in windows and reopen WSL for the change to be used:
wsl --shutdown
wsl -d nixos
TODO: maybe these changes can be added to the image before creating the WSL distro.
It is preferable to run NixOS containers with systemd-nspawn instead of Docker or Podman.
https://nixcademy.com/posts/nixos-nspawn/
Container image: https://github.com/tfc/nspawn-nixos
machinectl pull-tar https://github.com/tfc/nspawn-nixos/releases/download/v1.0/nixos-system-x86_64-linux.tar.xz nixos --verify=no
I get the error: Access denied.
Probably Github wants some access control?
OK, I download the 139 MB with the browser in Windows and then copy it to WSL/NixOS:
\\wsl.localhost\nixos\home\nixos\
The default user of this WSL image is called nixos
.
The pull-tar
command does just one thing: it copies the files into
/var/lib/machines/{container_name}
.
I can do that manually here in the NixOS shell.
cd ~
# [nixos@nixos:~]$
sudo mkdir /var/lib/machines/nixos
sudo tar -xvf nixos-system-x86_64-linux.tar.xz -C /var/lib/machines/nixos/
sudo ls -la /var/lib/machines/nixos
# run it in the background
sudo machinectl start nixos
sudo machinectl shell nixos /usr/bin/env passwd
TODO: How to create a new user? The default is nixos
and that is defined in the /etc/nixos/configuration.nix
file.
machinectl status nixos
machinectl login nixos
- Press ^] three times within 1s to exit session On my keyboard layout I have to use Ctrl+5 three times in one second. nixos login: root password : ***
nixos-version 23.11.20230826.5237477 (Tapir)
A simple exit
command in the container calls logout
and then continues to ask
nixos login:
Use three times Ctrl+5 to really exit the container.
enable internet access is to share the host’s network On the host: create /etc/systemd/nspawn/nixos.nspawn with content [Network] VirtualEthernet=no
After changing the file, reboot the container: machinectl reboot nixos
Now, we can edit the NixOS configuration file /etc/nixos/configuration.nix in the container’s file system. We can do that either from inside the container or from the host, as the container paths are all below /var/lib/machines/. For the configuration file, the full host path is /var/lib/machines/nixos/etc/nixos/configuration.nix.
nixos-rebuild switch
My open-source projects are free as a beer (MIT license).
I just love programming.
But I need also to drink. If you find my projects and tutorials helpful, please buy me a beer by donating to my PayPal.
You know the price of a beer in your local bar ;-)
So I can drink a free beer for your health :-)
Na zdravje! Alla salute! Prost! Nazdravlje! 🍻
//bestia.dev
//github.com/bestia-dev
//bestiadev.substack.com
//youtube.com/@bestia-dev-tutorials