Answers: <p>A closer reading of <a href="https://circleci.com/docs/2.0/caching/#using-keys-and-templates" rel="nofollow noreferrer">the documentation about using environment variables in cache keys</a> reveals the issue (emphasis mine):</p>
  
  <blockquote>
    <p>The environment variable variableName (supports any environment variable exported by CircleCI or added to a specific Context—<strong>not any arbitrary environment variable</strong>).</p>
  </blockquote>
  
  <p>"exported by CircleCI" seems to mean the list of mostly-<code>CIRCLE_...</code>-prefixed variables (documented as "<a href="https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables" rel="nofollow noreferrer">Built-In Environment Variables</a>") while (unsupported) "any arbitrary environment variable" includes variables set in an <code>environment</code> section in <code>config.yml</code>.</p>
  
  <p>I ended up working around this limitation something like this:</p>
  
  <pre><code>jobs:
    build:
      docker:
        - image: "nixorg/nix:circleci"
  
      environment:
        NIXPKGS_REV: "3c83ad6ac13b67101cc3e2e07781963a010c1624"
  
      steps:
        - "run":
            name: "Prepare For Cache Key"
            command: |
              echo "${NIXPKGS_REV}" &gt; nixpkgs.rev
  
        - restore_cache:
            name: "Restore Nix Store Paths"
            keys:
              - paymentserver-nix-store-v1-{{ checksum "nixpkgs.rev" }}
              - paymentserver-nix-store-v1-
  </code></pre>
  
  <p>That is, instead of trying to use the value of <code>NIXPKGS_REV</code> 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 <em>contain</em> 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.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-09-16T14:12:48.593Z
Answers: 2019-09-16T14:12:48.593Z
Answers: Jean-Paul Calderone
Body: <p>Using this configuration:</p>
  
  <pre><code>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-
  </code></pre>
  
  <p>The job tries to restore the cache using the key <code>paymentserver-nix-store-v1-&lt;no value&gt;</code>.</p>
  
  <p><a href="https://circleci.com/docs/2.0/env-vars/#setting-an-environment-variable-in-a-job" rel="nofollow noreferrer">https://circleci.com/docs/2.0/env-vars/#setting-an-environment-variable-in-a-job</a> says this is the correct way to set an environment variable.  <a href="https://circleci.com/docs/2.0/caching/#using-keys-and-templates" rel="nofollow noreferrer">https://circleci.com/docs/2.0/caching/#using-keys-and-templates</a> says interpolating environment variables this way is supported for cache keys.</p>
  
  <p>What's going on?  How do I use the value of an environment variable in a cache key?</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-09-16T14:28:00.007Z
Comments: I did refund your bounty as it should get back to you for the definitive answer ;)
Comments: Tensibai
Tags: <circleci-2.0>
Title: How to get other than "<no value>" when interpolating {{ .Environment.SOME_VAR }} (with SOME_VAR set) into cache key?
UserDisplayName: Jean-Paul Calderone
ThreadText: # How to get other than "<no value>" when interpolating {{ .Environment.SOME_VAR }} (with SOME_VAR set) into cache key?
  
  (Asked by: username_0 on 2019-09-11)
  
  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?
  
  **Tags:** circleci-2.0
  
  > **Comments:**
  > **username_1**: I did refund your bounty as it should get back to you for the definitive answer ;)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-09-16. Score: 1 | Accepted answer
  
  A closer reading of [the documentation about using environment variables in cache keys](https://circleci.com/docs/2.0/caching/#using-keys-and-templates) 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](https://circleci.com/docs/2.0/env-vars/#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.
  
  
  ---
  
  