2

Using this configuration:

version: 2
jobs:
  test:
    docker:
    - image: nixorg/nix:circleci
    environment:
      NIXPKGS_REV: 3c83ad6ac13b67101cc3e2e07781963a010c1624
    steps:
    - restore_cache:
        name: Restore Nix Store Paths
        keys:
        - paymentserver-nix-store-v1-{{ .Environment.NIXPKGS_REV }}
        - paymentserver-nix-store-v1-

The job tries to restore the cache using the key paymentserver-nix-store-v1-<no value>.

https://circleci.com/docs/2.0/env-vars/#setting-an-environment-variable-in-a-job says this is the correct way to set an environment variable. https://circleci.com/docs/2.0/caching/#using-keys-and-templates says interpolating environment variables this way is supported for cache keys.

What's going on? How do I use the value of an environment variable in a cache key?

1
  • I did refund your bounty as it should get back to you for the definitive answer ;)
    – Tensibai
    Sep 16 '19 at 14:28
1

A closer reading of the documentation about using environment variables in cache keys reveals the issue (emphasis mine):

The environment variable variableName (supports any environment variable exported by CircleCI or added to a specific Context—not any arbitrary environment variable).

"exported by CircleCI" seems to mean the list of mostly-CIRCLE_...-prefixed variables (documented as "Built-In Environment Variables") while (unsupported) "any arbitrary environment variable" includes variables set in an environment section in config.yml.

I ended up working around this limitation something like this:

jobs:
  build:
    docker:
      - image: "nixorg/nix:circleci"

    environment:
      NIXPKGS_REV: "3c83ad6ac13b67101cc3e2e07781963a010c1624"

    steps:
      - "run":
          name: "Prepare For Cache Key"
          command: |
            echo "${NIXPKGS_REV}" > nixpkgs.rev

      - restore_cache:
          name: "Restore Nix Store Paths"
          keys:
            - paymentserver-nix-store-v1-{{ checksum "nixpkgs.rev" }}
            - paymentserver-nix-store-v1-

That is, instead of trying to use the value of NIXPKGS_REV from the environment in the cache key, an earlier step writes the value of that environment variable to a file and then the cache key is constructed using the checksum of that file. Computing file checksums for use in cache keys is well supported so this produces a cache key which is derived from the value of the environment variable. The key doesn't contain the value of the environment variable but since the key's only purpose is to be unique for certain unique inputs, using the checksum works just as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.