Skip to content

Commit

Permalink
Test recomposing source with submodules
Browse files Browse the repository at this point in the history
oddly this doesn't reproduce the problem I'm experiencing IRL where
"dirtying" the submodule has no effect until the cache is either nuked
or runs out of TTL
  • Loading branch information
Shay Bergmann committed Nov 30, 2021
1 parent be57798 commit 74e7bcb
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions tests/flakes-with-submodules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@ for repo in $flakeDir $nonFlakeDir $flakeWithSubmodules; do
git -C $repo init
git -C $repo config user.email "[email protected]"
git -C $repo config user.name "Foobar"
done

for dir in $nonFlakeDir $flakeDir; do
cat > $dir/README.md <<EOF
FNORD
EOF
git -C $dir add README.md
git -C $dir commit -m 'Initial'
echo FNORD > $repo/README.md
git -C $repo add README.md
git -C $repo commit -m 'Initial'
done

cp config.nix $flakeDir
Expand Down Expand Up @@ -81,6 +76,11 @@ cat > $flakeWithSubmodules/flake.nix <<EOF
nonFlake = builtins.fetchTree (traceVal self.modules.nonFlake);
flake = builtins.getFlake (traceVal self.modules.flake);
mapAttrsToList = f: attrs:
map (name: f name attrs.\${name}) (builtins.attrNames attrs);
pipe = val: functions:
let reverseApply = x: f: f x;
in builtins.foldl' reverseApply val functions;
writeShellScriptBin = name: script: mkDerivation {
inherit name;
buildCommand = ''
Expand All @@ -91,12 +91,35 @@ cat > $flakeWithSubmodules/flake.nix <<EOF
chmod +x \$bin
'';
};
combineModules = self: let
srcs = builtins.mapAttrs (_: url: fetchTree (traceVal url)) self.modules;
in mkDerivation {
name = "source-with-submodules";
buildCommand = ''
cp -r \${self} \$out
chmod -R +w \$_
\${
pipe srcs [
(mapAttrsToList (path: src: "cp -rT \${src} \$out/\${path}"))
(builtins.concatStringsSep "\n")
]
}
'';
};
in {
packages.$system = {
cat-submodule-readme = writeShellScriptBin "cat-submodule-readme" ''
cat \${nonFlake}/README.md
'';
use-submodule-as-flake = flake.packages.$system.cat-own-readme;
cat-own-readme = writeShellScriptBin "cat-own-readme" ''
cat \${self}/README.md
'';
source-with-submodules = combineModules self;
};
defaultPackage.$system = self.packages.$system.cat-submodule-readme;
Expand All @@ -107,12 +130,19 @@ EOF
git -C $flakeWithSubmodules add .
git -C $flakeWithSubmodules commit -m'add flake.nix'

[[ $(nix run $flakeWithSubmodules#cat-own-readme) == "FNORD" ]]
[[ $(nix run $flakeWithSubmodules#cat-submodule-readme) == "FNORD" ]]
[[ $(nix run $flakeWithSubmodules#use-submodule-as-flake) == "FNORD" ]]
nix build -o $TEST_ROOT/result $flakeWithSubmodules#source-with-submodules
[[ $(cat $TEST_ROOT/result/nonFlake/README.md) == "FNORD" ]]

echo FOST > $flakeWithSubmodules/README.md

# apply dirt
echo FSUD > $flakeWithSubmodules/nonFlake/README.md
[[ $(nix run $flakeWithSubmodules#cat-submodule-readme) == "FSUD" ]]
nix build -o $TEST_ROOT/result $flakeWithSubmodules#source-with-submodules
[[ $(cat $TEST_ROOT/result/nonFlake/README.md) == "FSUD" ]]

# should work for flake as well
echo FSUD > $flakeWithSubmodules/flake/README.md
Expand Down

0 comments on commit 74e7bcb

Please sign in to comment.