Skip to content

Specify access token via file #6536

Open

Description

@Sohalt

Is your feature request related to a problem? Please describe.
I need to specify a github acess-token, if I want to include private repositories as flake inputs. I manage my /etc/nix/nix.conf declaratively on NixOS.

Describe the solution you'd like
I would like to keep the access token in a separate file using something like agenix or sops, to not have it end up in the nix store and in version control.
An option like acess-tokens-file = /run/secrets/access-tokens would be nice.
Ideally I'd be able to specify different access tokens in different files, e.g.
acess-tokens-files = github.com=/run/secrets/github-acess-token gitlab.com=/run/secrets/gitlab-acess-token

Describe alternatives you've considered

  • Add access-token to ǹix.extraOptions`
    • the token ends up in the nix store (and version control)
  • Add access-token to ~/.config/nix/nix.conf
  • This works, if I don't manage user config using home-manager, but it has the downside, that I have a separate secret to keep track of. It would be more convenient to be able to keep the secrets with all other secrets in agenix and use an access-tokens-files option in /etc/nix/nix.conf

Activity

  1. thufschmitt commented on May 16, 2022

    @thufschmitt
    Member

    Rather than a separate option, we could adopt the same syntax as builders (builders = @/some/file). Maybe even make this a more generic thing

  2. Sohalt commented on May 16, 2022

    @Sohalt
    Author

    That works, if the token never starts with an ´@´. But if that's the case, I'd like the more generic solution.

    I guess another option would be to use include /run/secrets/secret-config, although that way the file has to contain more than just the access token.

  3. colemickens commented on Aug 10, 2022

    @colemickens
    Member

    I'd also very much like to be able to specify these as separate file references, I guess maybe something like this?

    access-tokens = github.com=@/run/secrets/github-acess-token gitlab.com=@/run/secrets/gitlab-acess-token
    

    Any idea how hard this would be to implement? I'd like to be able to throw sops-nix at this since I'm hitting GH rate limits frequently lately.

  4. thufschmitt commented on Aug 11, 2022

    @thufschmitt
    Member

    maybe something like this?

    access-tokens = github.com=@/run/secrets/github-acess-token gitlab.com=@/run/secrets/gitlab-acess-token

    I guess that'd only work if the tokens can't start with an @, no idea whether that's possible or not. I'd rather go with access-token = @/run/secrets/github-acess-token @/run/secrets/gitlab-acess-token where each file contain the full key-value pair.

  5. rapenne-s commented on Sep 22, 2022

    @rapenne-s
    Member

    I'd also very much like to be able to specify these as separate file references, I guess maybe something like this?

    access-tokens = github.com=@/run/secrets/github-acess-token gitlab.com=@/run/secrets/gitlab-acess-token
    

    Any idea how hard this would be to implement? I'd like to be able to throw sops-nix at this since I'm hitting GH rate limits frequently lately.

    After thinking about this today, it would be a bad idea to add a semantic to include file content directly into the configuration file value.

    However, this would require more work but I think would play a lot nicer with automation and a clear syntax would be to use attributes sets, in which .file would expect a path to read the content and assign to the value. But I have no idea if this is easily doable, nix.conf syntax doesn't seem to allow this easily at first glance.

    access-tokens.github.com.file = /run/secrets/github-access-token
    
  6. jlesquembre commented on Sep 22, 2022

    @jlesquembre
    Member

    It's possible to include other files in nix.conf with include, an example using sops-nix (extracted from my dotfiles):

    {
      nix = {
        extraOptions = ''
          experimental-features = nix-command flakes
          !include ${config.sops.secrets.nixAccessTokens.path}
        '';
      };
    
      sops.secrets.nixAccessTokens = {
        mode = "0440";
        group = config.users.groups.keys.name;
      };
    }

    Notice the ! before the include. A missing file is an error without it. When you run nixos-rebuild switch, nix.conf is validated, but before sops-nix creates the secret file.

    Also, notice that the user running the nix command needs read access to the secret file.

    What is not possible with include is to have different tokens in different files, the last access-token declaration overwrites the previous ones.

  7. Kha commented on Sep 22, 2022

    @Kha
    Contributor

    What is not possible with include is to have different tokens in different files, the last access-token declaration overwrites the previous ones.

    That's what extra-access-tokens is for, right?

  8. jlesquembre commented on Sep 22, 2022

    @jlesquembre
    Member

    That's what extra-access-tokens is for, right?

    👍 Right, I missed the extra- part in nix.conf docs. Problem solved, thanks!

  9. SuperSandro2000 commented on Sep 22, 2022

    @SuperSandro2000
    Member

    That works, if the token never starts with an ´@´. But if that's the case, I'd like the more generic solution.

    New github tokens always start with ghp_ and gh*_ for enterprise and old ones where alphadecimal IIRC.

    It's possible to include other files in nix.conf with include, an example using sops-nix (extracted from my dotfiles):

    Noise, trying that out right now.

  10. tomberek commented on Dec 14, 2022

    @tomberek
    Contributor

    Somewhat related, would anyone be interested more specific url matching for the tokens?

  11. Freed-Wu commented on Jul 8, 2023

    @Freed-Wu
    Contributor

    Why not get access-token from other files like ~/.git-credentials or ~/.netrc?

    ~/.git-credentials

    https://user_name:[email protected]
    

    ~/.netrc

    machine api.github.com
        login user_name
        password gho_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    
  12. 15 remaining items

  13. axodentally commented on Apr 13, 2026

    @axodentally

    I would argue that #6536 (comment) is enough and we don't need an extra feature for this.

    I disagree, because of

    Notice the ! before the include. A missing file is an error without it. When you run nixos-rebuild switch, nix.conf is validated, but before sops-nix creates the secret file.

    One of the nice things in nix is having build-time failures instead of run-time ones when configuring your system: having an {,extra-}access-tokens-file option could allow relying on an include directive instead of an !include one, making the system less prone to runtime surprises.

    To add another point: I just put in the raw token in the sops-nix secret file and that did not work, since it has to be a the config line access-tokens = github.com=github_pat_TOKEN. Since the secret file is read as part of the configuration file for nix, every nix command failed with an error that put the secret in plaintext in the terminal.
    With this behavior, the secret can more easily leak.

  14. con-f-use commented on Apr 13, 2026

    @con-f-use

    I guess that'd only work if the tokens can't start with an @, no idea whether that's possible or not.

    If they start with an "at" @, we could also just introduce escaping, i.e. @something where "something" is a path and \@something where "something" is a token literal.

    To add another point: I just put in the raw token in the sops-nix secret file and that did not work, since it has to be a the config line access-tokens = github.com=github_pat_TOKEN.

    That, I believe, is what sops templating is for.

  15. added 2 commits that reference this issue on Aug 13, 2026
    eb9ea2a
    6b5e7dc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions