From 5452c8c638b59b5d6b123d81d3fa6bad07d00617 Mon Sep 17 00:00:00 2001 From: Nathan Henrie Date: Tue, 18 Oct 2022 12:50:14 -0600 Subject: [PATCH 1/2] Add support for submodules in flakes Fixes https://github.com/LnL7/nix-darwin/issues/549 --- pkgs/nix-tools/darwin-rebuild.sh | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/pkgs/nix-tools/darwin-rebuild.sh b/pkgs/nix-tools/darwin-rebuild.sh index a57fad7d..530ff829 100644 --- a/pkgs/nix-tools/darwin-rebuild.sh +++ b/pkgs/nix-tools/darwin-rebuild.sh @@ -121,9 +121,16 @@ if [ -z "$action" ]; then showSyntax; fi flakeFlags=(--extra-experimental-features 'nix-command flakes') if [ -n "$flake" ]; then - if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then - flake="${BASH_REMATCH[1]}" - flakeAttr="${BASH_REMATCH[2]}" + # Offical regex from https://www.rfc-editor.org/rfc/rfc3986#appendix-B + if [[ "${flake}" =~ ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? ]]; then + scheme=${BASH_REMATCH[2]} + authority=${BASH_REMATCH[4]} + path=${BASH_REMATCH[5]} + queryWithQuestion=${BASH_REMATCH[6]} + fragment=${BASH_REMATCH[9]} + + flake=${scheme}${authority}${path}${queryWithQuestion} + flakeAttr=${fragment} fi if [ -z "$flakeAttr" ]; then flakeAttr=$(hostname -s) @@ -138,7 +145,16 @@ if [ -n "$flake" ]; then cmd=info fi - flake=$(nix "${flakeFlags[@]}" flake "$cmd" --json "${extraMetadataFlags[@]}" "${extraLockFlags[@]}" -- "$flake" | jq -r .url) + metadata=$(nix "${flakeFlags[@]}" flake "$cmd" --json "${extraMetadataFlags[@]}" "${extraLockFlags[@]}" -- "$flake") + flake=$(jq -r .url <<<"${metadata}") + + if [ "$(jq -r .resolved.submodules <<<"${metadata}")" = "true" ]; then + if [[ "$flake" == *'?'* ]]; then + flake="${flake}&submodules=1" + else + flake="${flake}?submodules=1" + fi + fi fi if [ "$action" != build ] && [ -z "$flake" ]; then From e3c24b5e0c5e82f930125e0a3a99fd2608394d9e Mon Sep 17 00:00:00 2001 From: Nathan Henrie Date: Tue, 13 Dec 2022 15:35:22 -0700 Subject: [PATCH 2/2] Test submodule support --- .github/workflows/test.yml | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 86f318ec..cab17e89 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -64,3 +64,53 @@ jobs: - run: | . /etc/static/bashrc darwin-rebuild build --flake ./modules/examples#simple --override-input darwin . + - name: Test git submodules + run: | + . /etc/static/bashrc + + mkdir -p /tmp/{test-nix-darwin-submodules,example-submodule} + + pushd /tmp/example-submodule + echo '"hello"' > hello.nix + git init + git add . + git commit -m "add a submodule we will import" + popd + + cp -a ./modules/examples/. /tmp/test-nix-darwin-submodules + + pushd /tmp/test-nix-darwin-submodules + /usr/bin/sed -i.bak \ + '\#modules = \[#s#darwin.darwinModules.simple#./simple.nix#' \ + ./flake.nix + /usr/bin/sed -i.bak \ + 's#pkgs.vim#pkgs."${import ./submodule-test/hello.nix}"#' \ + ./simple.nix + git init + git add flake.nix simple.nix + git \ + -c protocol.file.allow=always \ + submodule add /tmp/example-submodule submodule-test + popd + + # Should fail + darwin-rebuild build \ + --flake /tmp/test-nix-darwin-submodules#simple \ + --override-input darwin . \ + && { + printf 'succeeded while expecting failure due to submodule\n' >/dev/stderr + exit 1 + } + # Should also fail + darwin-rebuild build \ + --flake /tmp/test-nix-darwin-submodules?submodules=0#simple \ + --override-input darwin . \ + && { + printf 'succeeded while expecting failure due to submodule\n' >/dev/stderr + exit 1 + } + + # Should succeed + darwin-rebuild build \ + --flake /tmp/test-nix-darwin-submodules?submodules=1#simple \ + --override-input darwin .