Skip to content

Commit

Permalink
Move most options to a module.
Browse files Browse the repository at this point in the history
Create module.nix with most of the contents of configuration.nix behind
an `enable` option. Import that module and enable the option in
configuration.nix to retain backwards compatibility. This allows the
flake to be consumed by other system configurations without doing
conditional imports.
  • Loading branch information
gcoakes committed Mar 5, 2022
1 parent 0b30e5d commit 9ef459e
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 56 deletions.
60 changes: 45 additions & 15 deletions build-tarball.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

with lib;
let
cfg = config.wsl.tarball;

pkgs2storeContents = l: map (x: { object = x; symlink = "none"; }) l;

nixpkgs = lib.cleanSource pkgs.path;
Expand Down Expand Up @@ -38,37 +40,65 @@ let
rm ./nix-path-registration
./$system/sw/bin/nix-env --store `pwd` -p ./nix/var/nix/profiles/system --set $system
${if cfg.setChannel then setChannel else ""}
# It's now a NixOS!
touch ./etc/NIXOS
${if cfg.copyConfig then copyConfig else ""}
'';

setChannel = ''
# Set channel
mkdir -p ./nix/var/nix/profiles/per-user/root
./$system/sw/bin/nix-env --store `pwd` -p ./nix/var/nix/profiles/per-user/root/channels --set ${channelSources}
mkdir -m 0700 -p ./root/.nix-defexpr
ln -s /nix/var/nix/profiles/per-user/root/channels ./root/.nix-defexpr/channels
'';

# It's now a NixOS!
touch ./etc/NIXOS
copyConfig = ''
# Copy the system configuration
mkdir -p ./etc/nixos
cp ${./configuration.nix} ./etc/nixos/configuration.nix
cp ${./module.nix} ./etc/nixos/module.nix
cp ${./syschdemd.nix} ./etc/nixos/syschdemd.nix
cp ${./syschdemd.sh} ./etc/nixos/syschdemd.sh
'';
in
{
system.build.tarball = pkgs.callPackage "${nixpkgs}/nixos/lib/make-system-tarball.nix" {
# No contents, structure will be added by prepare script
contents = [ ];
options.wsl.tarball = {
enable = mkOption {
type = types.bool;
default = true;
description = "Whether to allow building a tarball which can be imported into wsl.";
};
setChannel = mkOption {
type = types.bool;
default = true;
description = "Whether to set the channels within the tarball.";
};
copyConfig = mkOption {
type = types.bool;
default = true;
description = "Whether to copy the system configuration into the tarball.";
};
};
config = mkIf cfg.enable {
system.build.tarball = pkgs.callPackage "${nixpkgs}/nixos/lib/make-system-tarball.nix" {
# No contents, structure will be added by prepare script
contents = [ ];

storeContents = pkgs2storeContents [
config.system.build.toplevel
channelSources
preparer
];
storeContents = pkgs2storeContents [
config.system.build.toplevel
channelSources
preparer
];

extraCommands = "${preparer}/bin/wsl-prepare";
extraCommands = "${preparer}/bin/wsl-prepare";

# Use gzip
compressCommand = "gzip";
compressionExtension = ".gz";
# Use gzip
compressCommand = "gzip";
compressionExtension = ".gz";
};
};
}
44 changes: 3 additions & 41 deletions configuration.nix
Original file line number Diff line number Diff line change
@@ -1,46 +1,8 @@
{ lib, pkgs, config, modulesPath, ... }:

with lib;
let
defaultUser = "nixos";
syschdemd = import ./syschdemd.nix { inherit lib pkgs config defaultUser; };
in
{
{ lib, pkgs, config, modulesPath, ... }: {
imports = [
"${modulesPath}/profiles/minimal.nix"
./module.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;

users.users.${defaultUser} = {
isNormalUser = true;
extraGroups = [ "wheel" ];
};

users.users.root = {
shell = "${syschdemd}/bin/syschdemd";
# Otherwise WSL fails to login as root with "initgroups failed 5"
extraGroups = [ "root" ];
};

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;
wsl.enable = true;
}
3 changes: 3 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

outputs = { self, nixpkgs, flake-utils, ... }:
{
nixosModule = { ... }: {
imports = [ ./module.nix ./build-tarball.nix ];
};
nixosConfigurations.mysystem = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
Expand Down
52 changes: 52 additions & 0 deletions module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{ lib, pkgs, config, modulesPath, ... }:
let
cfg = config.wsl;
syschdemd = import ./syschdemd.nix {
inherit lib pkgs config;
inherit (cfg) defaultUser;
};
in
{
options.wsl = {
enable = lib.mkEnableOption "wsl";
defaultUser = lib.mkOption {
type = lib.types.str;
default = "nixos";
};
};
config = lib.mkIf cfg.enable {
# 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;

users.users.${cfg.defaultUser} = {
isNormalUser = true;
extraGroups = [ "wheel" ];
};

users.users.root = {
shell = "${syschdemd}/bin/syschdemd";
# Otherwise WSL fails to login as root with "initgroups failed 5"
extraGroups = [ "root" ];
};

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;
};
}

0 comments on commit 9ef459e

Please sign in to comment.