From 4262e1f600ca3d8317f095bb8e511e5243fe0238 Mon Sep 17 00:00:00 2001 From: nzbr Date: Tue, 1 Feb 2022 04:01:13 +0100 Subject: move build-tarball to a flake module --- build-tarball.nix | 74 ----------------------------------------------- configuration.nix | 6 +++- default.nix | 13 +++++++++ flake.lock | 17 +++++++++++ flake.nix | 19 +++++++++--- modules/build-tarball.nix | 74 +++++++++++++++++++++++++++++++++++++++++++++++ nixos.nix | 7 +---- 7 files changed, 125 insertions(+), 85 deletions(-) delete mode 100644 build-tarball.nix create mode 100644 default.nix create mode 100644 modules/build-tarball.nix diff --git a/build-tarball.nix b/build-tarball.nix deleted file mode 100644 index cbeb1dd..0000000 --- a/build-tarball.nix +++ /dev/null @@ -1,74 +0,0 @@ -{ config, pkgs, lib, nixpkgs ? , ... }: - -with lib; -let - pkgs2storeContents = l: map (x: { object = x; symlink = "none"; }) l; - - nixpkgs = lib.cleanSource pkgs.path; - - channelSources = pkgs.runCommand "nixos-${config.system.nixos.version}" - { preferLocalBuild = true; } - '' - mkdir -p $out - cp -prd ${nixpkgs.outPath} $out/nixos - chmod -R u+w $out/nixos - 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 - ''; - - preparer = pkgs.writeShellScriptBin "wsl-prepare" '' - set -e - - mkdir -m 0755 ./bin ./etc - mkdir -m 1777 ./tmp - - # WSL requires a /bin/sh - only temporary, NixOS's activate will overwrite - ln -s ${config.users.users.root.shell} ./bin/sh - - # WSL also requires a /bin/mount, otherwise the host fs isn't accessible - ln -s /nix/var/nix/profiles/system/sw/bin/mount ./bin/mount - - # Set system profile - system=${config.system.build.toplevel} - ./$system/sw/bin/nix-store --store `pwd` --load-db < ./nix-path-registration - rm ./nix-path-registration - ./$system/sw/bin/nix-env --store `pwd` -p ./nix/var/nix/profiles/system --set $system - - # 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 - - # 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 - ''; -in -{ - 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 - ]; - - extraCommands = "${preparer}/bin/wsl-prepare"; - - # Use gzip - compressCommand = "gzip"; - compressionExtension = ".gz"; - }; -} diff --git a/configuration.nix b/configuration.nix index c64aa06..05df82f 100644 --- a/configuration.nix +++ b/configuration.nix @@ -5,10 +5,13 @@ let defaultUser = "nixos"; automountPath = "/mnt"; syschdemd = import ./syschdemd.nix { inherit lib pkgs config defaultUser; }; + nixos-wsl = import ./default.nix; in { - imports = [ + imports = with nixos-wsl.nixosModules; [ "${modulesPath}/profiles/minimal.nix" + + build-tarball ]; # WSL is closer to a container than anything else @@ -68,4 +71,5 @@ in ${pkgs.rsync}/bin/rsync -ar --delete $systemConfig/sw/share/$x/. /usr/share/$x done ''; + }; } diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..873ece4 --- /dev/null +++ b/default.nix @@ -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 diff --git a/flake.lock b/flake.lock index 74d3e6b..48993f7 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,21 @@ { "nodes": { + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1641205782, + "narHash": "sha256-4jY7RCWUoZ9cKD8co0/4tFARpWB+57+r1bLLvXNJliY=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "b7547d3eed6f32d06102ead8991ec52ab0a4f1a7", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, "flake-utils": { "locked": { "lastModified": 1623875721, @@ -32,6 +48,7 @@ }, "root": { "inputs": { + "flake-compat": "flake-compat", "flake-utils": "flake-utils", "nixpkgs": "nixpkgs" } diff --git a/flake.nix b/flake.nix index 9e44244..b094c2b 100644 --- a/flake.nix +++ b/flake.nix @@ -1,19 +1,30 @@ { 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 = { + build-tarball = import ./modules/build-tarball.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 diff --git a/modules/build-tarball.nix b/modules/build-tarball.nix new file mode 100644 index 0000000..c91e9c5 --- /dev/null +++ b/modules/build-tarball.nix @@ -0,0 +1,74 @@ +{ config, pkgs, lib, ... }: +with builtins; with lib; +let + pkgs2storeContents = l: map (x: { object = x; symlink = "none"; }) l; + + nixpkgs = lib.cleanSource pkgs.path; + + channelSources = pkgs.runCommand "nixos-${config.system.nixos.version}" + { preferLocalBuild = true; } + '' + mkdir -p $out + cp -prd ${nixpkgs.outPath} $out/nixos + chmod -R u+w $out/nixos + if [ ! -e $out/nixos/nixpkgs ]; then + ln -s . $out/nixos/nixpkgs + fi + 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" '' + set -e + + mkdir -m 0755 ./bin ./etc + mkdir -m 1777 ./tmp + + # WSL requires a /bin/sh - only temporary, NixOS's activate will overwrite + ln -s ${config.users.users.root.shell} ./bin/sh + + # WSL also requires a /bin/mount, otherwise the host fs isn't accessible + ln -s /nix/var/nix/profiles/system/sw/bin/mount ./bin/mount + + # Set system profile + system=${config.system.build.toplevel} + ./$system/sw/bin/nix-store --store `pwd` --load-db < ./nix-path-registration + rm ./nix-path-registration + ./$system/sw/bin/nix-env --store `pwd` -p ./nix/var/nix/profiles/system --set $system + + # 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 + + # Copy the system configuration + mkdir -p ./etc/nixos + cp -R ${../.}/. ./etc/nixos/ + ''; + +in +{ + 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-${config.system.nixos.release}-${pkgs.hostPlatform.system}"; + + storeContents = pkgs2storeContents [ + config.system.build.toplevel + channelSources + preparer + ]; + + extraCommands = "${preparer}/bin/wsl-prepare"; + + # Use gzip + compressCommand = "gzip"; + compressionExtension = ".gz"; + }; +} diff --git a/nixos.nix b/nixos.nix index 3a32460..f9e850f 100644 --- a/nixos.nix +++ b/nixos.nix @@ -2,12 +2,7 @@ # nix-build -A system -A config.system.build.tarball ./nixos.nix import { - configuration = { - imports = [ - ./configuration.nix - ./build-tarball.nix - ]; - }; + configuration = import ./configuration.nix; system = "x86_64-linux"; } -- cgit v1.2.3