From c99dd77cc21507dbf7a23316448e21b2afb14fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Ma=C5=84ko?= Date: Fri, 18 Apr 2025 18:12:37 +0200 Subject: [PATCH] Use `nix-portable` to allow using the devshell without installing DD-enabled nix globally --- flake.lock | 299 +++++++++++++++++++++++++- flake.nix | 40 +++- nix/packages/nix-portable/default.nix | 54 +++++ 3 files changed, 391 insertions(+), 2 deletions(-) create mode 100644 nix/packages/nix-portable/default.nix diff --git a/flake.lock b/flake.lock index 2c75cd7..4d91d74 100644 --- a/flake.lock +++ b/flake.lock @@ -100,6 +100,38 @@ "type": "github" } }, + "flake-compat_4": { + "flake": false, + "locked": { + "lastModified": 1733328505, + "narHash": "sha256-NeCCThCEP3eCl2l/+27kNNK7QrwZB1IJCrXfrbv5oqU=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "ff81ac966bb2cae68946d5ed5fc4994f96d0ffec", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-compat_5": { + "flake": false, + "locked": { + "lastModified": 1733328505, + "narHash": "sha256-NeCCThCEP3eCl2l/+27kNNK7QrwZB1IJCrXfrbv5oqU=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "ff81ac966bb2cae68946d5ed5fc4994f96d0ffec", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, "flake-parts": { "inputs": { "nixpkgs-lib": [ @@ -121,6 +153,49 @@ "type": "github" } }, + "flake-parts_2": { + "inputs": { + "nixpkgs-lib": [ + "nix-dynamic-derivations", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1733312601, + "narHash": "sha256-4pDvzqnegAfRkPwO3wmwBhVi/Sye1mzps0zHWYnP88c=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "205b12d8b7cd4802fbcb8e8ef6a0f1408781a4f9", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "flake-parts_3": { + "inputs": { + "nixpkgs-lib": [ + "nix-portable", + "nix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1733312601, + "narHash": "sha256-4pDvzqnegAfRkPwO3wmwBhVi/Sye1mzps0zHWYnP88c=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "205b12d8b7cd4802fbcb8e8ef6a0f1408781a4f9", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, "git-hooks-nix": { "inputs": { "flake-compat": [ @@ -152,6 +227,72 @@ "type": "github" } }, + "git-hooks-nix_2": { + "inputs": { + "flake-compat": [ + "nix-dynamic-derivations" + ], + "gitignore": [ + "nix-dynamic-derivations" + ], + "nixpkgs": [ + "nix-dynamic-derivations", + "nixpkgs" + ], + "nixpkgs-stable": [ + "nix-dynamic-derivations", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1734279981, + "narHash": "sha256-NdaCraHPp8iYMWzdXAt5Nv6sA3MUzlCiGiR586TCwo0=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "aa9f40c906904ebd83da78e7f328cd8aeaeae785", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, + "git-hooks-nix_3": { + "inputs": { + "flake-compat": [ + "nix-portable", + "nix" + ], + "gitignore": [ + "nix-portable", + "nix" + ], + "nixpkgs": [ + "nix-portable", + "nix", + "nixpkgs" + ], + "nixpkgs-stable": [ + "nix-portable", + "nix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1734279981, + "narHash": "sha256-NdaCraHPp8iYMWzdXAt5Nv6sA3MUzlCiGiR586TCwo0=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "aa9f40c906904ebd83da78e7f328cd8aeaeae785", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, "globset": { "inputs": { "nixpkgs-lib": [ @@ -219,6 +360,57 @@ "type": "github" } }, + "nix-dynamic-derivations": { + "inputs": { + "flake-compat": "flake-compat_4", + "flake-parts": "flake-parts_2", + "git-hooks-nix": "git-hooks-nix_2", + "nixpkgs": [ + "nixpkgs" + ], + "nixpkgs-23-11": "nixpkgs-23-11_2", + "nixpkgs-regression": "nixpkgs-regression_3" + }, + "locked": { + "lastModified": 1740165078, + "narHash": "sha256-yqIVbJY7HkMjwZBoji0ptLuJpXUt94uk5zs0Dogt19c=", + "owner": "NixOS", + "repo": "nix", + "rev": "d904921eecbc17662fef67e8162bd3c7d1a54ce0", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nix", + "rev": "d904921eecbc17662fef67e8162bd3c7d1a54ce0", + "type": "github" + } + }, + "nix-portable": { + "inputs": { + "defaultChannel": [ + "nixpkgs" + ], + "nix": "nix_2", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1744990698, + "narHash": "sha256-S9ymJqViggsviY12+z1shsax8ilE2r842zoPodl5vYk=", + "owner": "jaen", + "repo": "nix-portable", + "rev": "ce7229a8b279d706dbd0e20131fcbe4e50d40c6e", + "type": "github" + }, + "original": { + "owner": "jaen", + "ref": "improvements", + "repo": "nix-portable", + "type": "github" + } + }, "nix2container": { "flake": false, "locked": { @@ -235,6 +427,29 @@ "type": "github" } }, + "nix_2": { + "inputs": { + "flake-compat": "flake-compat_5", + "flake-parts": "flake-parts_3", + "git-hooks-nix": "git-hooks-nix_3", + "nixpkgs": "nixpkgs_3", + "nixpkgs-23-11": "nixpkgs-23-11_3", + "nixpkgs-regression": "nixpkgs-regression_4" + }, + "locked": { + "lastModified": 1742824067, + "narHash": "sha256-rBPulEBpn4IiqkPsetuh7BRzT2iGCzZYnogTAsbrvhU=", + "owner": "NixOS", + "repo": "nix", + "rev": "9cb662df7442a1e2c4600fb8ecb2ad613ebc5a95", + "type": "github" + }, + "original": { + "id": "nix", + "ref": "2.27.1", + "type": "indirect" + } + }, "nixpkgs": { "locked": { "lastModified": 1735651292, @@ -267,6 +482,38 @@ "type": "github" } }, + "nixpkgs-23-11_2": { + "locked": { + "lastModified": 1717159533, + "narHash": "sha256-oamiKNfr2MS6yH64rUn99mIZjc45nGJlj9eGth/3Xuw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a62e6edd6d5e1fa0329b8653c801147986f8d446", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a62e6edd6d5e1fa0329b8653c801147986f8d446", + "type": "github" + } + }, + "nixpkgs-23-11_3": { + "locked": { + "lastModified": 1717159533, + "narHash": "sha256-oamiKNfr2MS6yH64rUn99mIZjc45nGJlj9eGth/3Xuw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a62e6edd6d5e1fa0329b8653c801147986f8d446", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a62e6edd6d5e1fa0329b8653c801147986f8d446", + "type": "github" + } + }, "nixpkgs-regression": { "locked": { "lastModified": 1643052045, @@ -299,6 +546,38 @@ "type": "github" } }, + "nixpkgs-regression_3": { + "locked": { + "lastModified": 1643052045, + "narHash": "sha256-uGJ0VXIhWKGXxkeNnq4TvV3CIOkUJ3PAoLZ3HMzNVMw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + } + }, + "nixpkgs-regression_4": { + "locked": { + "lastModified": 1643052045, + "narHash": "sha256-uGJ0VXIhWKGXxkeNnq4TvV3CIOkUJ3PAoLZ3HMzNVMw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + } + }, "nixpkgs_2": { "locked": { "lastModified": 1734359947, @@ -316,6 +595,22 @@ } }, "nixpkgs_3": { + "locked": { + "lastModified": 1734359947, + "narHash": "sha256-1Noao/H+N8nFB4Beoy8fgwrcOQLVm9o4zKW1ODaqK9E=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "48d12d5e70ee91fe8481378e540433a7303dbf6a", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "release-24.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_4": { "locked": { "lastModified": 1741310760, "narHash": "sha256-aizILFrPgq/W53Jw8i0a1h1GZAAKtlYOrG/A5r46gVM=", @@ -356,7 +651,9 @@ "globset": "globset", "lix": "lix", "nix": "nix", - "nixpkgs": "nixpkgs_3" + "nix-dynamic-derivations": "nix-dynamic-derivations", + "nix-portable": "nix-portable", + "nixpkgs": "nixpkgs_4" } } }, diff --git a/flake.nix b/flake.nix index fa3b04a..a19fbb4 100644 --- a/flake.nix +++ b/flake.nix @@ -4,6 +4,17 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + nix-dynamic-derivations = { + url = "github:NixOS/nix?rev=d904921eecbc17662fef67e8162bd3c7d1a54ce0"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + nix-portable ={ + url = "github:jaen/nix-portable/improvements"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.defaultChannel.follows = "nixpkgs"; + }; + nix.url = "github:hinshun/nix/2.27.1-fix-nix-missing-includes"; lix.url = "github:lix-project/lix/2.91.1"; @@ -42,6 +53,10 @@ inherit (pkgs) lib; + nixDdPortable = pkgs.callPackage ./nix/packages/nix-portable { + inherit inputs; + }; + craneLib = inputs.crane.mkLib pkgs; src = lib.fileset.toSource { @@ -188,7 +203,9 @@ packages.${system} = { inherit nix-ninja nix-ninja-task; - inherit (pkgs) nix; + inherit nixDdPortable; + + nix = nixDdPortable; default = nix-ninja; @@ -207,10 +224,31 @@ devShells.${system}.default = craneLib.devShell { checks = inputs.self.checks.${system}; + shellHook = '' + if command -v direnv_layout_dir 2>&1 >/dev/null; then + export NP_LOCATION="$(direnv_layout_dir)" + mkdir -p "$NP_LOCATION" + + ADDITIONAL_CONFIG_FILE="$NP_LOCATION/nix-additional.conf" + rm $ADDITIONAL_CONFIG_FILE + fi + + if command -v git 2>&1 >/dev/null; then + export NP_GIT="$(which git)" + fi + + echo "max-jobs = auto" >> "$ADDITIONAL_CONFIG_FILE" + echo "cores = 0" >> "$ADDITIONAL_CONFIG_FILE" + + export NP_CONF_ADDITIONAL_CONFIG="$ADDITIONAL_CONFIG_FILE" + export NP_CONF_ADDITIONAL_FEATURES="ca-derivations dynamic-derivations recursive-nix" + ''; + packages = with pkgs; [ gnumake just meson + nixDdPortable ]; }; }; diff --git a/nix/packages/nix-portable/default.nix b/nix/packages/nix-portable/default.nix new file mode 100644 index 0000000..a237000 --- /dev/null +++ b/nix/packages/nix-portable/default.nix @@ -0,0 +1,54 @@ +{ + lib, + pkgs, + inputs, + stdenv, + runCommand, + ... +}: + let + system = stdenv.system; + + ddPackages = inputs.nix-dynamic-derivations.packages.${system}; + + nixDd = ddPackages.nix-everything; + nixDdStatic = ddPackages.nix-everything-static; + + nixPortable = pkgs.callPackage inputs.nix-portable { + inherit lib pkgs; + + buildSystem = system; + dataDirName = ".nix-dd-portable"; + + # The one from nixpkgs doesn't seem to work + proot = import "${ inputs.nix-portable }/proot/alpine.nix" { inherit pkgs; }; + + nix = nixDd; + nixStatic = nixDdStatic; + busybox = pkgs.pkgsStatic.busybox; + bubblewrap = pkgs.pkgsStatic.bubblewrap; + gnutar = pkgs.pkgsStatic.gnutar; + perl = pkgs.pkgsBuildBuild.perl; + xz = pkgs.pkgsStatic.xz; + zstd = pkgs.pkgsStatic.zstd; + }; + + wrapped = runCommand "nix-bin" {} '' + mkdir -p $out/bin + + ln -s ${ nixPortable }/bin/nix-portable $out/bin/nix + + ln -s $out/bin/nix $out/bin/nix-build + ln -s $out/bin/nix $out/bin/nix-channel + ln -s $out/bin/nix $out/bin/nix-collect-garbage + ln -s $out/bin/nix $out/bin/nix-copy-closure + ln -s $out/bin/nix $out/bin/nix-daemon + ln -s $out/bin/nix $out/bin/nix-env + ln -s $out/bin/nix $out/bin/nix-hash + ln -s $out/bin/nix $out/bin/nix-instantiate + ln -s $out/bin/nix $out/bin/nix-prefetch-url + ln -s $out/bin/nix $out/bin/nix-shell + ln -s $out/bin/nix $out/bin/nix-store + ''; + in + wrapped \ No newline at end of file