NixOS on WSL2
https://github.com/nix-community/nixos-wsl を使ってNixOS on WSL2 をする。
-
Releases から
nixos-wsl.tar.gz
をダウンロードしてくる $ wsl --import <OS名> --version 2 <インストール先のパス> <ダウンロードしたtar.gzファイルのパス>
自分の場合は、
$ wsl --import NixOS --version 2 ~/wsl-distributions/NixOS ~/Downloads/nixos-wsl.tar.gz
wsl -d NixOS
または (Windows Terminalを使ってるなら)ターミナルを再起動して新しいタブを開く
から選択するとディストリを開ける
Welcome to your new NixOS-WSL system!
Please run `sudo nix-channel --update` and `sudo nixos-rebuild switch` now, to ensure you're running the latest NixOS and NixOS-WSL versions.
If you run into issues, please report them on our Github page at https://github.com/nix-community/NixOS-WSL or come talk to us on Matrix at #wsl:nixos.org.
❄️ Enjoy NixOS-WSL! ❄️
Note: this message will disappear after you rebuild your system. If you want to see it again, run `nixos-wsl-welcome`.
[nixos@nixos:~]$
言われたとおりに
sudo nix-channel --update
sudo nixos-rebuild switch
sudo nix-channel --update
を実行直後何も起きてない感じになるけど待つ
sudo nixos-rebuild switch
でめっちゃログが出てくる
とりあえず PATH からWindows側のパスを排除したい。補完が重い
$ cat /etc/wsl.conf
[automount]
enabled=true
ldconfig=false
mountFsTab=false
options=metadata,uid=1000,gid=100
root=/mnt
[boot]
command=
systemd=true
[interop]
appendWindowsPath=true
enabled=true
[network]
generateHosts=true
generateResolvConf=true
hostname=nixos
[user]
default=nixos
appendWindowsPath=true
をfalse
にしたいけど、sudo nano
で編集したら読み取り専用らしい。
sudo nano
で編集したら読み取り専用らしい。
ls でリンクを辿ってみた。
[nixos@nixos:~]$ ls -l /etc/wsl.conf
lrwxrwxrwx 1 root root 20 Sep 23 13:55 /etc/wsl.conf -> /etc/static/wsl.conf
[nixos@nixos:~]$ ls -l /etc/static/wsl.conf
lrwxrwxrwx 1 root root 56 Jan 1 1970 /etc/static/wsl.conf -> /nix/store/2ap4wl6xji7v1l5d0v8j3j8l1hfb70ny-etc-wsl.conf
[nixos@nixos:~]$ ls -l /nix/store/2ap4wl6xji7v1l5d0v8j3j8l1hfb70ny-etc-wsl.conf
-r--r--r-- 1 root root 269 Jan 1 1970 /nix/store/2ap4wl6xji7v1l5d0v8j3j8l1hfb70ny-etc-wsl.conf
シンボリックリンクの根っこまでたどるとこんな感じだった。Nix環境であることを実感
NixOS-WSLのドキュメントを発見 (ちゃんと見よう)
https://nix-community.github.io/NixOS-WSL/options.html#wslinteropincludepath にそれっぽい設定項目があるので、以下のように/etc/nixos/configuration.nix
を書き換える
# Edit this configuration file to define what should be installed on
# your system. Help is available in the configuration.nix(5) man page, on
# https://search.nixos.org/options and in the NixOS manual (`nixos-help`).
# NixOS-WSL specific options are documented on the NixOS-WSL repository:
# https://github.com/nix-community/NixOS-WSL
{ config, lib, pkgs, ... }:
{
imports = [
# include NixOS-WSL modules
<nixos-wsl/modules>
];
wsl.enable = true;
wsl.defaultUser = "nixos";
+ wsl.interop.includePath = false;
# This value determines the NixOS release from which the default
# settings for stateful data, like file locations and database versions
# on your system were taken. It's perfectly fine and recommended to leave
# this value at the release version of the first install of this system.
# Before changing this value read the documentation for this option
# (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
system.stateVersion = "24.05"; # Did you read the comment?
}
WSLを落として(wsl --shutdown
)、ディストリを起動しなおすと$PATH
からwin側のパスが消えてくれる。
ちなみに、/etc/wsl.conf
のappendWindowsPath
は依然としてfalse
のまま。
使う分にはさっきの方法で問題ないけど気持ち悪いので、/etc/wsl.conf
側も書き換えるように設定する。
-
公式ドキュメントに
wsl.wslConf.interop.appendWindowsPath
な項目がある -
sudo nano /etc/nixos/configuration.nix
で1行追記する
# Edit this configuration file to define what should be installed on
# your system. Help is available in the configuration.nix(5) man page, on
# https://search.nixos.org/options and in the NixOS manual (`nixos-help`).
# NixOS-WSL specific options are documented on the NixOS-WSL repository:
# https://github.com/nix-community/NixOS-WSL
{ config, lib, pkgs, ... }:
{
imports = [
# include NixOS-WSL modules
<nixos-wsl/modules>
];
wsl.enable = true;
wsl.defaultUser = "nixos";
wsl.interop.includePath = false;
+ wsl.wslConf.interop.appendWindowsPath = false;
# This value determines the NixOS release from which the default
# settings for stateful data, like file locations and database versions
# on your system were taken. It's perfectly fine and recommended to leave
# this value at the release version of the first install of this system.
# Before changing this value read the documentation for this option
# (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
system.stateVersion = "24.05"; # Did you read the comment?
}
sudo nixos-rebuild switch
WSLを再起動する必要はないらしく、wsl.conf
を見ると書き換わっていることを確認。
$ cat /etc/wsl.conf
[automount]
enabled=true
ldconfig=false
mountFsTab=false
options=metadata,uid=1000,gid=100
root=/mnt
[boot]
command=
systemd=true
[interop]
appendWindowsPath=false # <= ここがfalseになった
enabled=true
[network]
generateHosts=true
generateResolvConf=true
hostname=nixos
[user]
default=nixos
ユーザ名を変える
# Edit this configuration file to define what should be installed on
# your system. Help is available in the configuration.nix(5) man page, on
# https://search.nixos.org/options and in the NixOS manual (`nixos-help`).
# NixOS-WSL specific options are documented on the NixOS-WSL repository:
# https://github.com/nix-community/NixOS-WSL
{ config, lib, pkgs, ... }:
{
imports = [
# include NixOS-WSL modules
<nixos-wsl/modules>
];
wsl.enable = true;
- wsl.defaultUser = "nixos";
+ wsl.defaultUser = "motch";
wsl.interop.includePath = false;
wsl.wslConf.interop.appendWindowsPath = false;
# This value determines the NixOS release from which the default
# settings for stateful data, like file locations and database versions
# on your system were taken. It's perfectly fine and recommended to leave
# this value at the release version of the first install of this system.
# Before changing this value read the documentation for this option
# (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
system.stateVersion = "24.05"; # Did you read the comment?
}
$ sudo nixos-rebuild boot
$ exit
$ wsl -t NixOS
$ wsl -d NixOS --user root exit
$ wsl -t NixOS
/home/nixos
が残ってたから sudo rm -rf /home.nixos
で消した。
ユーザ名は変更できてたのでヨシ
https://zenn.dev/asa1984/articles/nixos-is-the-best#環境構築 をみながらflakes化してみる。
ファイル
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs = inputs: {
nixosConfigurations = {
myNixOS = inputs.nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./configuration.nix
];
};
};
};
}
# Edit this configuration file to define what should be installed on
# your system. Help is available in the configuration.nix(5) man page, on
# https://search.nixos.org/options and in the NixOS manual (`nixos-help`).
# NixOS-WSL specific options are documented on the NixOS-WSL repository:
# https://github.com/nix-community/NixOS-WSL
{ config, lib, pkgs, ... }:
{
imports = [
# include NixOS-WSL modules
<nixos-wsl/modules>
];
wsl.enable = true;
wsl.defaultUser = "motch";
wsl.interop.includePath = false;
wsl.wslConf.interop.appendWindowsPath = false;
nix = {
settings = {
experimental-features = ["nix-command" "flakes"];
};
};
# This value determines the NixOS release from which the default
# settings for stateful data, like file locations and database versions
# on your system were taken. It's perfectly fine and recommended to leave
# this value at the release version of the first install of this system.
# Before changing this value read the documentation for this option
# (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
system.stateVersion = "24.05"; # Did you read the comment?
}
[motch@nixos:~/nix-config]$ sudo nixos-rebuild switch --flake .#myNixOS
error:
… while calling the 'seq' builtin
at /nix/store/5n0kgjjm07z3gvz912i5gviig1qh4d4w-source/lib/modules.nix:334:18:
333| options = checked options;
334| config = checked (removeAttrs config [ "_module" ]);
| ^
335| _module = checked (config._module);
… while evaluating a branch condition
at /nix/store/5n0kgjjm07z3gvz912i5gviig1qh4d4w-source/lib/modules.nix:273:9:
272| checkUnmatched =
273| if config._module.check && config._module.freeformType == null && merged.unmatchedDefns != [] then
| ^
274| let
(stack trace truncated; use '--show-trace' to show the full trace)
error: cannot look up '<nixos-wsl/modules>' in pure evaluation mode (use '--impure' to override)
at «none»:0: (source not available)
--impure
オプションをつけろってことらしいけど、Nixのことだし多分自分が間違ってそう
https://github.com/nix-community/NixOS-WSL/discussions/374 これか?
diff
[motch@nixos:~/nix-config]$ diff -u _flake.nix flake.nix
--- _flake.nix 2024-09-24 13:14:16.898024808 +0000
+++ flake.nix 2024-09-24 13:40:27.678940943 +0000
@@ -1,14 +1,16 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
+ nixos-wsl.url = "github:nix-community/nixos-wsl";
};
- outputs = inputs: {
+ outputs = { self, nixpkgs, nixos-wsl }: {
nixosConfigurations = {
- myNixOS = inputs.nixpkgs.lib.nixosSystem {
+ myNixOS = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./configuration.nix
+ nixos-wsl.nixosModules.wsl
];
};
};
[motch@nixos:~/nix-config]$ diff -u _configuration.nix configuration.nix
--- _configuration.nix 2024-09-24 13:38:53.289673717 +0000
+++ configuration.nix 2024-09-24 13:33:05.361272910 +0000
@@ -8,11 +8,6 @@
{ config, lib, pkgs, ... }:
{
- imports = [
- # include NixOS-WSL modules
- <nixos-wsl/modules>
- ];
-
wsl.enable = true;
wsl.defaultUser = "motch";
wsl.interop.includePath = false;
@@ -23,7 +18,7 @@
experimental-features = ["nix-command" "flakes"];
};
};
-
+
# This value determines the NixOS release from which the default
# settings for stateful data, like file locations and database versions
[motch@nixos:~/nix-config]$ sudo nixos-rebuild switch --flake .#myNixOS
warning: updating lock file '/home/motch/nix-config/flake.lock':
• Added input 'nixos-wsl':
'github:nix-community/nixos-wsl/1fcec53c692c15091ca5bb9eaf86a2cac6c53278' (2024-09-23)
• Added input 'nixos-wsl/flake-compat':
'github:edolstra/flake-compat/0f9255e01c2351cc7d116c072cb317785dd33b33' (2023-10-04)
• Added input 'nixos-wsl/flake-utils':
'github:numtide/flake-utils/c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a' (2024-09-17)
• Added input 'nixos-wsl/flake-utils/systems':
'github:nix-systems/default/da67096a3b9bf56a91d16901293e51ba5b49a27e' (2023-04-09)
• Added input 'nixos-wsl/nixpkgs':
'github:NixOS/nixpkgs/944b2aea7f0a2d7c79f72468106bc5510cbf5101' (2024-09-20)
building the system configuration...
activating the configuration...
setting up /run/booted-system...
setting up /etc...
setting up /bin...
setting up /bin/login...
setting up /sbin/init shim...
reloading user units for motch...
restarting sysinit-reactivation.target
reloading the following units: dbus.service
通った!
さっきのissueを真似して色々入れてみた。
[motch@nixos:~/nix-config]$ diff -u _configuration.nix configuration.nix
--- _configuration.nix 2024-09-24 13:38:53.289673717 +0000
+++ configuration.nix 2024-09-24 13:45:26.216705771 +0000
@@ -8,22 +8,21 @@
{ config, lib, pkgs, ... }:
{
- imports = [
- # include NixOS-WSL modules
- <nixos-wsl/modules>
- ];
-
wsl.enable = true;
wsl.defaultUser = "motch";
wsl.interop.includePath = false;
wsl.wslConf.interop.appendWindowsPath = false;
- nix = {
- settings = {
- experimental-features = ["nix-command" "flakes"];
- };
- };
-
+ nix.settings.experimental-features = ["nix-command" "flakes"];
+
+ environment.systemPackages = with pkgs; [
+ git
+ vim
+ eza
+ bat
+ delta
+ ];
+
# This value determines the NixOS release from which the default
# settings for stateful data, like file locations and database versions
$ sudo nixos-rebuiild switch --flake .#myNixOS
でezaもbatもdeltaもvimも入る。すごい
NixOS 爆破
$ wsl -t NixOS
$ wsl --unregister NixOS
作り直した。
$ wsl --import NixOS --version 2 ~/wsl-distributions/NixOS ~/Downloads/nixos-wsl.tar.gz
nixos-wsl-welcome
に言われた通りに以下のコマンドを実行。
$ sudo nix-channel --update
$ sudo nixos-rebuild switch
のために、/etc/nixos/configuration.nix
を書き換える。
$ sudo nano /etc/nixos/configuration.nix
[nixos@nixos:/etc/nixos]$ diff -u _configuration.nix configuration.nix
--- _configuration.nix 2024-09-26 04:25:13.591810574 +0000
+++ configuration.nix 2024-09-26 04:40:09.808411180 +0000
@@ -14,7 +14,11 @@
];
wsl.enable = true;
- wsl.defaultUser = "nixos";
+ wsl.defaultUser = "motch";
+ wsl.interop.includePath = false;
+
+ nix.settings.experimental-features = [ "nix-command" "flakes" ];
# This value determines the NixOS release from which the default
# settings for stateful data, like file locations and database versions
/etc/nixos/configuration.nix
# Edit this configuration file to define what should be installed on
# your system. Help is available in the configuration.nix(5) man page, on
# https://search.nixos.org/options and in the NixOS manual (`nixos-help`).
# NixOS-WSL specific options are documented on the NixOS-WSL repository:
# https://github.com/nix-community/NixOS-WSL
{ config, lib, pkgs, ... }:
{
imports = [
# include NixOS-WSL modules
<nixos-wsl/modules>
];
wsl.enable = true;
wsl.defaultUser = "motch";
wsl.interop.includePath = false;
nix.settings.experimental-features = [ "nix-command" "flakes" ];
# This value determines the NixOS release from which the default
# settings for stateful data, like file locations and database versions
# on your system were taken. It's perfectly fine and recommended to leave
# this value at the release version of the first install of this system.
# Before changing this value read the documentation for this option
# (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
system.stateVersion = "24.05"; # Did you read the comment?
}
NixOS-WSL でデフォルトユーザを変更する方法を行う。
$ sudo nixos-rebuild boot
$ wsl -t NixOS
$ wsl -d NixOS -u root exit
$ wsl -t NixOS
NixOS を起動して、デフォルトユーザが変わっているのを確認。
[motch@nixos:~]$ whoami
motch
変更前のユーザ(nixos
) は消えているがホームディレクトリ(/home/nixos
) が残ってる状態なので削除する。
$ cat /etc/passwd | grep nixos # 出力が空文字なので存在しない
$ sudo rm -rf /home/nixos
https://nixos-and-flakes.thiscute.world/nixos-with-flakes/nixos-with-flakes-enabled を見るとflakesを有効化するのにもう少し設定が必要らしい。
--- /etc/nixos/_configuration.nix 2024-09-26 05:30:39.656293168 +0000
+++ /etc/nixos/configuration.nix 2024-09-26 05:25:55.597207536 +0000
@@ -18,6 +18,7 @@
wsl.interop.includePath = false;
nix.settings.experimental-features = [ "nix-command" "flakes" ];
# This value determines the NixOS release from which the default
# settings for stateful data, like file locations and database versions
@@ -26,4 +27,13 @@
# Before changing this value read the documentation for this option
# (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
system.stateVersion = "24.05"; # Did you read the comment?
+
+ environment.systemPackages = with pkgs; [
+ git
+ vim
+ ];
+
+ environment.variables = {
+ EDITOR = "vim";
+ };
}
/etc/nixos/configuration.nix
# Edit this configuration file to define what should be installed on
# your system. Help is available in the configuration.nix(5) man page, on
# https://search.nixos.org/options and in the NixOS manual (`nixos-help`).
# NixOS-WSL specific options are documented on the NixOS-WSL repository:
# https://github.com/nix-community/NixOS-WSL
{ config, lib, pkgs, ... }:
{
imports = [
# include NixOS-WSL modules
<nixos-wsl/modules>
];
wsl.enable = true;
wsl.defaultUser = "motch";
wsl.interop.includePath = false;
nix.settings.experimental-features = [ "nix-command" "flakes" ];
# This value determines the NixOS release from which the default
# settings for stateful data, like file locations and database versions
# on your system were taken. It's perfectly fine and recommended to leave
# this value at the release version of the first install of this system.
# Before changing this value read the documentation for this option
# (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
system.stateVersion = "24.05"; # Did you read the comment?
environment.systemPackages = with pkgs; [
git
vim
];
environment.variables = {
EDITOR = "vim";
};
}
$ sudo nixos-rebuild switch
How to configure NixOS-WSL with flakes や 前のスクラップを見ながらファイルを加える。
--- /dev/fd/63 2024-09-26 06:03:36.138540640 +0000
+++ flake.nix 2024-09-26 05:58:15.367000659 +0000
@@ -1 +1,22 @@
+{
+ description = "A simple NixOS flake";
+ inputs = {
+ # NixOS official package source, using the nixos-23.11 branch here
+ nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
+ nixos-wsl.url = "github:nix-community/NixOS-WSL/main";
+ };
+
+ outputs = { self, nixpkgs, nixos-wsl, ... }@inputs: {
+ # Please replace my-nixos with your hostname
+ nixosConfigurations.nixos = nixpkgs.lib.nixosSystem {
+ system = "x86_64-linux";
+ modules = [
+ nixos-wsl.nixosModules.wsl
+ # Import the previous configuration.nix we used,
+ # so the old configuration file still takes effect
+ ./configuration.nix
+ ];
+ };
+ };
+}
/etc/nixos/flake.nix
{
description = "A simple NixOS flake";
inputs = {
# NixOS official package source, using the nixos-23.11 branch here
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
nixos-wsl.url = "github:nix-community/NixOS-WSL/main";
};
outputs = { self, nixpkgs, nixos-wsl, ... }@inputs: {
# Please replace my-nixos with your hostname
nixosConfigurations.nixos = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
nixos-wsl.nixosModules.wsl
# Import the previous configuration.nix we used,
# so the old configuration file still takes effect
./configuration.nix
];
};
};
}
--- _configuration.nix 2024-09-26 06:02:37.077595004 +0000
+++ configuration.nix 2024-09-26 05:58:37.375734516 +0000
@@ -9,8 +9,6 @@
{
imports = [
- # include NixOS-WSL modules
- <nixos-wsl/modules>
];
wsl.enable = true;
/etc/nixos/configuration.nix
# Edit this configuration file to define what should be installed on
# your system. Help is available in the configuration.nix(5) man page, on
# https://search.nixos.org/options and in the NixOS manual (`nixos-help`).
# NixOS-WSL specific options are documented on the NixOS-WSL repository:
# https://github.com/nix-community/NixOS-WSL
{ config, lib, pkgs, ... }:
{
imports = [
];
wsl.enable = true;
wsl.defaultUser = "motch";
wsl.interop.includePath = false;
nix.settings.experimental-features = [ "nix-command" "flakes" ];
# This value determines the NixOS release from which the default
# settings for stateful data, like file locations and database versions
# on your system were taken. It's perfectly fine and recommended to leave
# this value at the release version of the first install of this system.
# Before changing this value read the documentation for this option
# (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
system.stateVersion = "24.05"; # Did you read the comment?
environment.systemPackages = with pkgs; [
git
vim
];
environment.variables = {
EDITOR = "vim";
};
}
$ sudo nixos-rebuild switch
$ cd # ~ に移動
$ git init nix-config
$ cd nix-config
$ cp /etc/nixos/* .