ragenix

repository·main·Indexed 19 days ago

https://github.com/yaxitech/ragenix

A Rust-based drop-in replacement for agenix that provides age-encrypted secrets for NixOS and macOS systems. Designed specifically for Nix Flakes, ragenix allows users to create, edit, and rekey secrets using a rules file (typically secrets.nix) to map secret paths to authorized public keys. It features support for age plugins, robust CLI parsing, and compatibility with the agenix flake.

Tokens
4.8K
Snippets
14
Records
24
Agent score
65%

What's inside ragenix

  1. Overview of ragenix

    main

    ragenix is a Rust-based tool that provides age-encrypted secrets for NixOS systems. These secrets live in the Nix store and are decrypted during system activation. It is designed as a drop-in replacement for agenix, aiming for full compatibility with the agenix flake while providing more robust CLI parsing, validation, plugin support, and shell completions.

    Note: Unlike agenix, ragenix focuses exclusively on supporting Nix Flakes.

  2. Use the ragenix CLI

    main

    The ragenix CLI is used to create, edit, and rekey age-encrypted secrets. It follows a similar command-line pattern to agenix.

    Core Commands:

    • --edit <FILE>: Edits an age-encrypted file using your configured $EDITOR.
    • --rekey: Re-encrypts all secrets with the recipients specified in your rules.
    • --schema: Prints the JSON schema that your rules must conform to.
    # Edit a secret file
    ragenix --edit ./secrets/my-secret.age
    
    # Re-key secrets using defined rules
    ragenix --rekey
    
    # View the JSON schema for rules
    ragenix --schema
  3. Configure the Nix binary path via RAGENIX_NIX_BIN_PATH

    main

    Ragenix relies on a Nix binary to parse and evaluate rules files. You must ensure the path to your Nix binary is provided via the RAGENIX_NIX_BIN_PATH environment variable. The application uses this binary to execute nix eval --json on your rules files to convert Nix attribute sets into JSON for processing.

    export RAGENIX_NIX_BIN_PATH=/path/to/your/nix
  4. Enable recursive-nix for NixOS

    main

    If you are running tests or environments that require nix to run within itself (such as during CI or specific local development setups), you must enable the recursive-nix feature in your NixOS configuration.

    {
      nix = {
        extraOptions = ''
          experimental-features = nix-command flakes recursive-nix
        '';
        systemFeatures = [ "recursive-nix" ];
      };
    }
  5. Install ragenix

    main

    To install ragenix, replace any references to github.com/ryantm/agenix with github.com/yaxitech/ragenix in your existing agenix setup. The ragenix package provides aliases for both the agenix package and the agenix binary to ensure compatibility.

    You can install it using one of the following methods:

    • Nix Profile: nix profile install github:yaxitech/ragenix
    • NixOS: Add to environment.systemPackages in your configuration.
    • Home Manager: Add to home.packages.
    nix profile install github:yaxitech/ragenix
  6. Configure rules for secret encryption

    main

    Rules are defined in a Nix expression file (typically secrets.nix) that maps secret file paths to the public keys of authorized recipients.

    Rule Resolution:

    • Each secret file string in the Nix expression is considered relative to the parent directory of the rules file.
    • If the --rules option is omitted, ragenix looks for the RULES environment variable. If that is unset, it looks for secrets.nix in the current working directory.

    Example secrets.nix structure:

    {
      "secret.txt.age".publicKeys = [
        "age1g4eapz2lkdvrevsg443yx8rhxklhyz4sa8w0jdfyh8sgx3azhftsz8zu07"
        "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKrb9ne3nZjw6DW..."
      ];
    }
  7. Use the ragenix CLI to manage secrets

    main

    The ragenix CLI is used to validate, edit, and rekey secrets based on defined rules. The tool operates on a rules file that specifies how secrets are handled.

    Key operational modes include:

    • Schema Output: Print the JSON schema for agenix compatibility.
    • Validation: Validate the provided rules file for correctness.
    • Edit Mode: Open a specific file for editing using a specified editor. This mode requires the --edit flag and an editor (via --editor or the EDITOR environment variable).
    • Rekey Mode: Perform a rekey operation on all rules using the provided identities.
    # Example: Editing a specific file
    # Note: This assumes the CLI arguments are parsed correctly by the internal cli module
    # The --edit flag requires an editor to be specified
    # ragenix --rules <path_to_rules> --edit <path_to_file> --editor <editor_command>
    
    # Example: Rekeying all secrets
    # ragenix --rules <path_to_rules> --rekey
  8. Extend ragenix with age plugins

    main

    ragenix supports age plugins. If plugin binaries are present in your PATH, ragenix will pick them up automatically.

    To ensure plugins are always available (e.g., when using ragenix within a Nix derivation), use the plugins argument in the ragenix.override function to wrap the binary with an extended PATH.

    # Example: Making the age-plugin-yubikey available via Nix
    { ragenix, age-plugin-yubikey }: 
    
    ragenix.override { 
      plugins = [ age-plugin-yubikey ]; 
    }
  9. Rekey all secrets in a configuration

    main

    The -r or --rekey option decrypts all secrets defined in your rules configuration file and re-encrypts them using the specified public keys. This is primarily used to grant new recipients access to existing secrets.

    Key behaviors:

    • In-memory processing: To ensure security, ragenix does not write any plaintext data to disk during a rekey operation; all processing happens in-memory.
    • Existing files: If a secret file defined in the rules does not exist on disk, it is ignored.
    • Identities: If --identity is not provided, it uses default SSH private keys.
    # Rekey all secrets defined in ./secrets.nix using a specific age identity
    ragenix -i ~/.age/ragenix.key -r
  10. Edit or create an age-encrypted secret

    main

    Use the -e or --edit option to decrypt a secret file and open it in an editor. If the file does not exist, ragenix will create an empty one. The file path must match a rule defined in your configuration file. After you save and exit the editor, ragenix encrypts the content and replaces the original file.

    Key behaviors:

    • Decryption: If --identity is not provided, it attempts to use default SSH private keys (~/.ssh/id_ed25519 and ~/.ssh/id_rsa).
    • Security: Decrypted content is written to a temporary file accessible only by the current user and is deleted immediately after the editor exits.
    • Format: The resulting encrypted file is always in ASCII-armored format.
    # Edit a secret using the default rules file (./secrets.nix)
    ragenix -e secret.txt.age
    
    # Edit a secret using a specific rules file and a specific identity
    ragenix --rules /var/lib/secrets/rules.nix -e secret.txt.age
    
    # Stream-encrypt data from stdin to a secret file (no editor opened)
    # Use '-' as the editor to trigger stdin mode
    pbpaste | ragenix --editor - -e secret.txt.age
  11. Configure ragenix via environment variables

    main

    You can configure several ragenix behaviors using environment variables to avoid passing them as flags every time:

    • EDITOR: Sets the command used to edit files when using the --edit action.
    • RULES: Sets the path to the Nix file that specifies recipient public keys (defaults to ./secrets.nix).
  12. Reference: ragenix CLI options

    main

    The following options are available for the ragenix command:

    • -e, --edit <FILE>: Edits the age-encrypted FILE using $EDITOR.
    • --editor <EDITOR>: Specifies the editor to use when editing FILE (defaults to EDITOR environment variable, e.g., vim).
    • -h, --help: Print help information.
    • -i, --identity <PRIVATE_KEY>...: Private key(s) to use when decrypting.
    • -r, --rekey: Re-encrypts all secrets with specified recipients.
    • --rules <RULES>: Path to a Nix file specifying recipient public keys. (Defaults to ./secrets.nix; can be set via RULES environment variable).
    • -s, --schema: Prints the JSON schema Agenix rules have to conform to.
    • -v, --verbose: Verbose output.
    • -V, --version: Print version information.
    USAGE:
        ragenix [OPTIONS] <--edit <FILE>|--rekey|--schema>
    
    OPTIONS:
        -e, --edit <FILE>                  edits the age-encrypted FILE using $EDITOR
            --editor <EDITOR>              editor to use when editing FILE [env: EDITOR=vim]
        -h, --help                         Print help information
        -i, --identity <PRIVATE_KEY>...    private key to use when decrypting
        -r, --rekey                        re-encrypts all secrets with specified recipients
            --rules <RULES>                path to Nix file specifying recipient public keys [env: 
                                           RULES=] [default: ./secrets.nix]
        -s, --schema                       Prints the JSON schema Agenix rules have to conform to
        -v, --verbose                      verbose output
        -V, --version                      Print version information