How to get the list of files to cache from a command?

I’ve just added a pipeline job to verify that the Nix shell that I have configured is runnable. I added caching to the job before realising that it’ll be caching much more than it should be. There’s a workaround, but it relies on getting the paths to be cached from a command, and I’m not sure if there’s a convenient way to do that in GitLab CI.