Using Nix flakes + Cachix + GitLab CI

Here is a partial solution thanks to @jojosch who helped me via Matrix chat:

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 383d80e..0caa9ce 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -13,18 +13,7 @@ dist:
         nixpkgs.cachix

   script:
-    - git diff-index HEAD
-    - git status
-    - git diff
-    - ls -ARplh
-    - git clean -dfx
-    - git diff-index HEAD
-    - git status
-    - git diff
-    - nix --experimental-features "nix-command flakes" flake metadata --json | jq
-    - git reset --hard HEAD
-    - git diff-index HEAD
-    - nix --experimental-features "nix-command flakes" flake metadata --json | jq
+    - nix --experimental-features "nix-command flakes" path-info --all | grep -v '\.drv$' | sort > /tmp/store-path-pre-build
     - make result
     - cp --recursive result/ dist/

diff --git a/Makefile b/Makefile
index d2dca35..d6b4c11 100644
--- a/Makefile
+++ b/Makefile
@@ -16,9 +16,8 @@ result: $(shell find src -type f)
 	$(nix) build --print-build-logs --verbose

 cache: result
-	$(nix) flake archive --json \
-	| jq --raw-output '.path, (.inputs | to_entries [] .value.path)' \
-	| cachix push software-garden
+	$(nix) path-info --all | grep -v '\.drv$$' | sort > /tmp/store-path-post-build
+	comm -13 /tmp/store-path-pre-build /tmp/store-path-post-build | cachix push software-garden

 .PHONY: develop
 develop: node-dependencies.nix node_modules

This propagates the cache from one machine (laptop) to another (GitLab CI) and it’s a big step forward and I really appreciate your help.

Unfortunately it doesn’t seem robust. For example in my first attempt I accidentally pushed a lot of unrelated paths including 400MB mesa build because I was running unrelated nix shell in another terminal. Wrong things can get pushed accidentally. This is not a problem if pushing from CI, because it’s containerized, but on development machine it is.

Also if I’m not mistaken, if I build the project and then build again and try to push the cache, nothing will get pushed, because on second attempt the paths are already in the store, right? And actually the most likely use case for me is building this project on my laptop before pushing to GitLab and saving time in CI.

So I want to figure out a way to identify and push the result of building this flake only. I thought that this is done as suggested by https://nixos.wiki/wiki/Flakes#Pushing_Flake_inputs_to_Cachix like that:

$ nix flake archive --json \
  | jq -r '.path,(.inputs|to_entries[].value.path)' \
  | cachix push $cache_name

So there is still some tinkering ahead of me :hammer_and_wrench:

Edit: does this make sense?

realpath result | cachix push software-garden