From 165caeb17325178c76f1786606d7ab721708a76e Mon Sep 17 00:00:00 2001 From: user3301 <26126682+user3301@users.noreply.github.com> Date: Sat, 15 Aug 2026 08:41:06 +0000 Subject: [PATCH 1/2] Clone WSL dotfiles during activation Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- docs/README.nix.md | 11 +++---- flake.nix | 54 ++++++++++++++++++++++++---------- scripts/bootstrap-nixos-wsl.sh | 21 +------------ 3 files changed, 45 insertions(+), 41 deletions(-) diff --git a/docs/README.nix.md b/docs/README.nix.md index 48eccb0..a315cc9 100644 --- a/docs/README.nix.md +++ b/docs/README.nix.md @@ -24,11 +24,12 @@ nix --extra-experimental-features 'nix-command flakes' run github:user3301/dotfi the distribution's virtual disk; `nixos-anywhere` is intended to provision an SSH target and normally repartition its disks. -This first creates `user3301` without activating Home Manager, clones the -repository to `/home/user3301/dotfiles`, and then activates the full -configuration from that checkout. It is safe to rerun after an interrupted -bootstrap. When it completes, run `wsl --shutdown` from PowerShell and reopen -the distribution. +The bootstrap configuration clones the repository to +`/home/user3301/dotfiles` as part of NixOS activation, before Home Manager +starts. This remains reliable even when changing WSL's default user terminates +the original shell session. It is safe to rerun after an interrupted bootstrap. +When it completes, run `wsl --shutdown` from PowerShell and reopen the +distribution. ### NixOS Native ```bash diff --git a/flake.nix b/flake.nix index a473674..77995f3 100644 --- a/flake.nix +++ b/flake.nix @@ -142,31 +142,55 @@ ./systems/wsl/configuration.nix ]; + wslHomeManagerModules = [ + home-manager.nixosModules.home-manager + { + home-manager = { + useGlobalPkgs = true; + useUserPackages = true; + users.user3301 = import ./home/nixos-wsl.nix; + extraSpecialArgs = { inherit inputs; }; + }; + } + ]; + in { # NixOS Configurations nixosConfigurations = { - # Minimal first-stage configuration used by the WSL bootstrap app. + # One-time installer that clones the mutable dotfiles checkout before + # Home Manager activates the same configuration as nixos-wsl. nixos-wsl-bootstrap = mkSystem { system = "x86_64-linux"; - modules = wslSystemModules; + modules = wslSystemModules ++ wslHomeManagerModules ++ [ + ( + { pkgs, ... }: + { + system.activationScripts.bootstrapDotfiles = { + deps = [ "users" ]; + text = '' + target=/home/user3301/dotfiles + if [[ ! -e "$target" ]]; then + echo "cloning dotfiles into $target..." + ${pkgs.coreutils}/bin/install -d -o user3301 -g users /home/user3301 + ${pkgs.util-linux}/bin/runuser -u user3301 -- \ + ${pkgs.git}/bin/git clone \ + https://github.com/user3301/dotfiles.git "$target" + elif [[ ! -d "$target/.git" ]]; then + echo "error: $target exists but is not a Git checkout" >&2 + exit 1 + fi + ''; + }; + } + ) + ]; }; # NixOS WSL2 Configuration nixos-wsl = mkSystem { system = "x86_64-linux"; - modules = wslSystemModules ++ [ - # Home Manager integration - home-manager.nixosModules.home-manager - { - home-manager = { - useGlobalPkgs = true; - useUserPackages = true; - users.user3301 = import ./home/nixos-wsl.nix; - extraSpecialArgs = { inherit inputs; }; - }; - } - ]; + modules = wslSystemModules ++ wslHomeManagerModules; }; # Native NixOS Configuration @@ -216,8 +240,6 @@ pkgs.writeShellApplication { name = "bootstrap-nixos-wsl"; runtimeInputs = with pkgs; [ - coreutils - git gnugrep ]; text = builtins.readFile ./scripts/bootstrap-nixos-wsl.sh; diff --git a/scripts/bootstrap-nixos-wsl.sh b/scripts/bootstrap-nixos-wsl.sh index d302964..eb91f68 100644 --- a/scripts/bootstrap-nixos-wsl.sh +++ b/scripts/bootstrap-nixos-wsl.sh @@ -1,19 +1,14 @@ set -euo pipefail flake_ref="${DOTFILES_FLAKE:-github:user3301/dotfiles}" -repo_url="${DOTFILES_REPO_URL:-https://github.com/user3301/dotfiles.git}" target_user="user3301" -target_home="/home/${target_user}" -target_repo="${target_home}/dotfiles" -git_bin="$(command -v git)" if [[ "${1:-}" == "--help" ]]; then cat <&2 - exit 1 -else - echo "Cloning dotfiles into ${target_repo}..." - sudo -H -u "${target_user}" "${git_bin}" clone "${repo_url}" "${target_repo}" -fi - -echo "Activating the configuration from the local checkout..." -sudo nixos-rebuild switch --flake "${target_repo}#nixos-wsl" - cat < Date: Sat, 15 Aug 2026 08:50:02 +0000 Subject: [PATCH 2/2] Format WSL bootstrap configuration Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- flake.nix | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/flake.nix b/flake.nix index 77995f3..185d7e4 100644 --- a/flake.nix +++ b/flake.nix @@ -162,29 +162,32 @@ # Home Manager activates the same configuration as nixos-wsl. nixos-wsl-bootstrap = mkSystem { system = "x86_64-linux"; - modules = wslSystemModules ++ wslHomeManagerModules ++ [ - ( - { pkgs, ... }: - { - system.activationScripts.bootstrapDotfiles = { - deps = [ "users" ]; - text = '' - target=/home/user3301/dotfiles - if [[ ! -e "$target" ]]; then - echo "cloning dotfiles into $target..." - ${pkgs.coreutils}/bin/install -d -o user3301 -g users /home/user3301 - ${pkgs.util-linux}/bin/runuser -u user3301 -- \ - ${pkgs.git}/bin/git clone \ - https://github.com/user3301/dotfiles.git "$target" - elif [[ ! -d "$target/.git" ]]; then - echo "error: $target exists but is not a Git checkout" >&2 - exit 1 - fi - ''; - }; - } - ) - ]; + modules = + wslSystemModules + ++ wslHomeManagerModules + ++ [ + ( + { pkgs, ... }: + { + system.activationScripts.bootstrapDotfiles = { + deps = [ "users" ]; + text = '' + target=/home/user3301/dotfiles + if [[ ! -e "$target" ]]; then + echo "cloning dotfiles into $target..." + ${pkgs.coreutils}/bin/install -d -o user3301 -g users /home/user3301 + ${pkgs.util-linux}/bin/runuser -u user3301 -- \ + ${pkgs.git}/bin/git clone \ + https://github.com/user3301/dotfiles.git "$target" + elif [[ ! -d "$target/.git" ]]; then + echo "error: $target exists but is not a Git checkout" >&2 + exit 1 + fi + ''; + }; + } + ) + ]; }; # NixOS WSL2 Configuration