A secret can be stored in a file. It is written at runtime in the configuration file. Note it is also possible to write them in the nix store for dev purposes.
···11+{ lib }:
22+33+with lib;
44+55+rec {
66+ # A shell script string helper to get the value of a secret at
77+ # runtime.
88+ getSecret = secretOption:
99+ if secretOption.storage == "fromFile"
1010+ then ''$(cat ${secretOption.value})''
1111+ else ''${secretOption.value}'';
1212+1313+1414+ # A shell script string help to replace at runtime in a file the
1515+ # pattern of a secret by its value.
1616+ replaceSecret = secretOption: filename: ''
1717+ sed -i "s/${secretOption.pattern}/${getSecret secretOption}/g" ${filename}
1818+ '';
1919+2020+ # This generates an option that can be used to declare secrets which
2121+ # can be stored in the nix store, or not. A pattern is written in
2222+ # the nix store to represent the secret. The pattern can
2323+ # then be overwritten with the value of the secret at runtime.
2424+ mkSecretOption = {name, description ? ""}:
2525+ mkOption {
2626+ description = description;
2727+ type = types.submodule ({
2828+ options = {
2929+ pattern = mkOption {
3030+ type = types.str;
3131+ default = "##${name}##";
3232+ description = "The pattern that represent the secret.";
3333+ };
3434+ storage = mkOption {
3535+ type = types.enum [ "fromNixStore" "fromFile" ];
3636+ description = ''
3737+ Choose the way the password is provisionned. If
3838+ fromNixStore is used, the value is the password and it is
3939+ written in the nix store. If fromFile is used, the value
4040+ is a path from where the password will be read at
4141+ runtime. This is generally used with <link
4242+ xlink:href="https://nixos.org/nixops/manual/#opt-deployment.keys">
4343+ deployment keys</link> of Nixops.
4444+ '';};
4545+ value = mkOption {
4646+ type = types.str;
4747+ description = ''
4848+ If the storage is fromNixStore, the value is the password itself,
4949+ otherwise it is a path to the file that contains the password.
5050+ '';
5151+ };
5252+ };});
5353+ };
5454+}