Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Everything that changed in my fork of this repo #56

Merged
merged 17 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use_flake() {
watch_file flake.nix
watch_file flake.lock
eval "$(nix print-dev-env)"
}

use flake
39 changes: 32 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@ jobs:
run: |
nix build '.#nixosConfigurations.mysystem.config.system.build.tarball'

- uses: actions/upload-artifact@v2
- name: Upload tarball
uses: actions/upload-artifact@v2
with:
name: install.tar.gz
path: result/tarball/nixos-system-x86_64-linux.tar.gz
name: rootfs
path: result/tarball/nixos-wsl-x86_64-linux.tar.gz

- name: Build installer
run: |
nix build '.#nixosConfigurations.mysystem.config.system.build.installer'

- name: Upload installer
uses: actions/upload-artifact@v2
with:
name: installer
path: result/tarball/nixos-wsl-installer.tar.gz

release:
if: startsWith(github.ref, 'refs/tags/')
Expand All @@ -43,7 +54,11 @@ jobs:

- uses: actions/download-artifact@v2
with:
name: install.tar.gz
name: rootfs

- uses: actions/download-artifact@v2
with:
name: installer

- name: Create Release
id: create_release
Expand All @@ -59,7 +74,17 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }} #
asset_name: nixos-system-x86_64-linux.tar.gz
asset_path: nixos-system-x86_64-linux.tar.gz
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_name: nixos-wsl-x86_64-linux.tar.gz
asset_path: result/tarball/nixos-wsl-x86_64-linux.tar.gz
asset_content_type: application/gzip

- name: Upload installer as release asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_name: nixos-wsl-installer.tar.gz
asset_path: result/tarball/nixos-wsl-installer.tar.gz
asset_content_type: application/gzip
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ First, `download the latest release's system tarball

Then open up a Terminal, PowerShell or Command Prompt and run::

wsl --import NixOS .\NixOS\ nixos-system-x86_64-linux.tar.gz --version 2
wsl --import NixOS .\NixOS\ nixos-wsl-x86_64-linux.tar.gz --version 2

This sets up a new WSL distribution ``NixOS`` that is installed under
``.\NixOS``. ``nixos-system-x86_64-linux.tar.gz`` is the path to the file you
``.\NixOS``. ``nixos-wsl-x86_64-linux.tar.gz`` is the path to the file you
downloaded earlier. You might need to change this path or change to the download
directory first.

Expand Down Expand Up @@ -72,10 +72,10 @@ Or, if you want to build with local changes, run inside your checkout::

Without a flakes-enabled Nix, you can build a tarball using::

nix-build -A system -A config.system.build.tarball ./nixos.nix
nix-build -A nixosConfigurations.mysystem.config.system.build.tarball

The resulting mini rootfs can then be found under
``./result-2/tarball/nixos-system-x86_64-linux.tar.gz``.
``./result/tarball/nixos-wsl-x86_64-linux.tar.gz``.


License
Expand Down
46 changes: 15 additions & 31 deletions configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,29 @@

with lib;
let
defaultUser = "nixos";
syschdemd = import ./syschdemd.nix { inherit lib pkgs config defaultUser; };
nixos-wsl = import ./default.nix;
in
{
imports = [
"${modulesPath}/profiles/minimal.nix"
];

# WSL is closer to a container than anything else
boot.isContainer = true;

environment.etc.hosts.enable = false;
environment.etc."resolv.conf".enable = false;

networking.dhcpcd.enable = false;
nixos-wsl.nixosModules.wsl
];

users.users.${defaultUser} = {
isNormalUser = true;
extraGroups = [ "wheel" ];
};
wsl = {
enable = true;
automountPath = "/mnt";
defaultUser = "nixos";
startMenuLaunchers = true;

users.users.root = {
shell = "${syschdemd}/bin/syschdemd";
# Otherwise WSL fails to login as root with "initgroups failed 5"
extraGroups = [ "root" ];
# Enable integration with Docker Desktop (needs to be installed)
# docker.enable = true;
};

security.sudo.wheelNeedsPassword = false;

# Disable systemd units that don't make sense on WSL
systemd.services."serial-getty@ttyS0".enable = false;
systemd.services."serial-getty@hvc0".enable = false;
systemd.services."getty@tty1".enable = false;
systemd.services."autovt@".enable = false;

systemd.services.firewall.enable = false;
systemd.services.systemd-resolved.enable = false;
systemd.services.systemd-udevd.enable = false;

# Don't allow emergency mode, because we don't have a console.
systemd.enableEmergencyMode = false;
# Enable nix flakes
nix.package = pkgs.nixFlakes;
nix.extraOptions = ''
experimental-features = nix-command flakes
'';
}
13 changes: 13 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(import
(
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{
src = ./.;
}).defaultNix
29 changes: 23 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
{
description = "NixOS WSL";

inputs.nixpkgs.url = "nixpkgs/nixos-20.09";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs = {
nixpkgs.url = "nixpkgs/nixos-20.09";
flake-utils.url = "github:numtide/flake-utils";

flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
};

outputs = { self, nixpkgs, flake-utils, ... }:
{
nixosModules.wsl = {
imports = [
./modules/build-tarball.nix
./modules/wsl-distro.nix
./modules/docker-desktop.nix
./modules/installer.nix
];
};

nixosConfigurations.mysystem = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
(import ./configuration.nix)
(import ./build-tarball.nix)
./configuration.nix
];
specialArgs = { inherit nixpkgs; };
};

} //
flake-utils.lib.eachDefaultSystem (system:
let
Expand Down
31 changes: 20 additions & 11 deletions build-tarball.nix → modules/build-tarball.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{ config, pkgs, lib, nixpkgs ? <nixpkgs>, ... }:

with lib;
{ config, pkgs, lib, ... }:
with builtins; with lib;
let
pkgs2storeContents = l: map (x: { object = x; symlink = "none"; }) l;

Expand All @@ -15,9 +14,9 @@ let
if [ ! -e $out/nixos/nixpkgs ]; then
ln -s . $out/nixos/nixpkgs
fi
echo -n ${config.system.nixos.revision} > $out/nixos/.git-revision
echo -n ${config.system.nixos.versionSuffix} > $out/nixos/.version-suffix
echo ${config.system.nixos.versionSuffix} | sed -e s/pre// > $out/nixos/svn-revision
echo -n ${toString config.system.nixos.revision} > $out/nixos/.git-revision
echo -n ${toString config.system.nixos.versionSuffix} > $out/nixos/.version-suffix
echo ${toString config.system.nixos.versionSuffix} | sed -e s/pre// > $out/nixos/svn-revision
'';

preparer = pkgs.writeShellScriptBin "wsl-prepare" ''
Expand Down Expand Up @@ -47,18 +46,27 @@ let
# It's now a NixOS!
touch ./etc/NIXOS

# Write wsl.conf so that it is present when NixOS is started for the first time
cp ${config.environment.etc."wsl.conf".source} ./etc/wsl.conf

# Copy the system configuration
mkdir -p ./etc/nixos
cp ${./configuration.nix} ./etc/nixos/configuration.nix
cp ${./syschdemd.nix} ./etc/nixos/syschdemd.nix
cp ${./syschdemd.sh} ./etc/nixos/syschdemd.sh
mkdir -p ./etc/nixos/nixos-wsl
cp -R ${lib.cleanSource ../.}/. ./etc/nixos/nixos-wsl
mv ./etc/nixos/nixos-wsl/configuration.nix ./etc/nixos/configuration.nix
# Patch the import path to avoid havin a flake.nix in /etc/nixos
sed -i 's|import \./default\.nix|import \./nixos-wsl|' ./etc/nixos/configuration.nix
'';

in
{
mkIf config.wsl.enable {
# These options make no sense without the wsl-distro module anyway

system.build.tarball = pkgs.callPackage "${nixpkgs}/nixos/lib/make-system-tarball.nix" {
# No contents, structure will be added by prepare script
contents = [ ];

fileName = "nixos-wsl-${pkgs.hostPlatform.system}";

storeContents = pkgs2storeContents [
config.system.build.toplevel
channelSources
Expand All @@ -71,4 +79,5 @@ in
compressCommand = "gzip";
compressionExtension = ".gz";
};

}
37 changes: 37 additions & 0 deletions modules/docker-desktop.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{ config, lib, pkgs, ... }:
with builtins; with lib; {

options.wsl.docker = with types; {
enable = mkEnableOption "Docker Desktop integration";
};

config =
let
cfg = config.wsl.docker;
in
mkIf (config.wsl.enable && cfg.enable) {

environment.systemPackages = with pkgs; [
docker
docker-compose
];

systemd.services.docker-desktop-proxy = {
description = "Docker Desktop proxy";
script = ''
${config.wsl.automountPath}/wsl/docker-desktop/docker-desktop-proxy -docker-desktop-root ${config.wsl.automountPath}/wsl/docker-desktop
'';
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Restart = "on-failure";
RestartSec = "30s";
};
};

users.groups.docker.members = [
config.wsl.defaultUser
];

};

}
Loading