Skip to content

Commit

Permalink
Merge #324: Fix lnd onion
Browse files Browse the repository at this point in the history
ecc601a onion-addresses: mirror nix-bitcoin.onionAddresses.access behavior (nixbitcoin)
e873326 modules: use user & group options (nixbitcoin)
ccef870 spark-wallet: add user & group options (nixbitcoin)
85a1722 lnd: add user & group options (nixbitcoin)

Pull request description:

ACKs for top commit:
  erikarvstedt:
    ACK ecc601a

Tree-SHA512: 39da5f8e01b98a676af8a073c11df64df487b5c3ab01327a227d16f215826f5bf15ca9ac21b59934edc5e2bbb87e397c53fcbf7130bd10b00f1df359ab3328ba
  • Loading branch information
jonasnick committed Feb 17, 2021
2 parents eddc48e + ecc601a commit bcad047
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 36 deletions.
8 changes: 4 additions & 4 deletions modules/bitcoind.nix
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,13 @@ in {
users.groups.bitcoinrpc = {};
nix-bitcoin.operator.groups = [ cfg.group ];

nix-bitcoin.secrets.bitcoin-rpcpassword-privileged.user = "bitcoin";
nix-bitcoin.secrets.bitcoin-rpcpassword-privileged.user = cfg.user;
nix-bitcoin.secrets.bitcoin-rpcpassword-public = {
user = "bitcoin";
user = cfg.user;
group = "bitcoinrpc";
};

nix-bitcoin.secrets.bitcoin-HMAC-privileged.user = "bitcoin";
nix-bitcoin.secrets.bitcoin-HMAC-public.user = "bitcoin";
nix-bitcoin.secrets.bitcoin-HMAC-privileged.user = cfg.user;
nix-bitcoin.secrets.bitcoin-HMAC-public.user = cfg.user;
};
}
8 changes: 4 additions & 4 deletions modules/btcpayserver.nix
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,18 @@ in {
users.groups.${cfg.nbxplorer.group} = {};
users.users.${cfg.btcpayserver.user} = {
group = cfg.btcpayserver.group;
extraGroups = [ "nbxplorer" ]
extraGroups = [ cfg.nbxplorer.group ]
++ optional (cfg.btcpayserver.lightningBackend == "clightning") cfg.clightning.user;
home = cfg.btcpayserver.dataDir;
};
users.groups.${cfg.btcpayserver.group} = {};

nix-bitcoin.secrets = {
bitcoin-rpcpassword-btcpayserver = {
user = "bitcoin";
group = "nbxplorer";
user = cfg.bitcoind.user;
group = cfg.nbxplorer.group;
};
bitcoin-HMAC-btcpayserver.user = "bitcoin";
bitcoin-HMAC-btcpayserver.user = cfg.bitcoind.user;
};
};
}
2 changes: 1 addition & 1 deletion modules/electrs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ in {

users.users.${cfg.user} = {
group = cfg.group;
extraGroups = [ "bitcoinrpc" ] ++ optionals cfg.high-memory [ "bitcoin" ];
extraGroups = [ "bitcoinrpc" ] ++ optionals cfg.high-memory [ bitcoind.user ];
};
users.groups.${cfg.group} = {};
};
Expand Down
8 changes: 4 additions & 4 deletions modules/lightning-loop.nix
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ in {
environment.systemPackages = [ cfg.package (hiPrio cfg.cli) ];

systemd.tmpfiles.rules = [
"d '${cfg.dataDir}' 0770 lnd lnd - -"
"d '${cfg.dataDir}' 0770 ${config.services.lnd.user} ${config.services.lnd.group} - -"
];

systemd.services.lightning-loop = {
Expand All @@ -98,7 +98,7 @@ in {
after = [ "lnd.service" ];
serviceConfig = nbLib.defaultHardening // {
ExecStart = "${cfg.package}/bin/loopd --configfile=${configFile}";
User = "lnd";
User = config.services.lnd.user;
Restart = "on-failure";
RestartSec = "10s";
ReadWritePaths = cfg.dataDir;
Expand All @@ -108,8 +108,8 @@ in {
};

nix-bitcoin.secrets = {
loop-key.user = "lnd";
loop-cert.user = "lnd";
loop-key.user = config.services.lnd.user;
loop-cert.user = config.services.lnd.user;
};
};
}
2 changes: 1 addition & 1 deletion modules/liquid.nix
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,6 @@ in {
users.groups.${cfg.group} = {};
nix-bitcoin.operator.groups = [ cfg.group ];

nix-bitcoin.secrets.liquid-rpcpassword.user = "liquid";
nix-bitcoin.secrets.liquid-rpcpassword.user = cfg.user;
};
}
2 changes: 1 addition & 1 deletion modules/lnd-rest-onion-service.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let
lnd = config.services.lnd;

bin = pkgs.writeScriptBin "lndconnect-rest-onion" ''
#!/usr/bin/env -S ${runAsUser} lnd ${pkgs.bash}/bin/bash
#!/usr/bin/env -S ${runAsUser} ${lnd.user} ${pkgs.bash}/bin/bash
exec ${cfg.package}/bin/lndconnect \
--host=$(cat ${config.nix-bitcoin.onionAddresses.dataDir}/lnd/lnd-rest) \
Expand Down
34 changes: 22 additions & 12 deletions modules/lnd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ in {
default = pkgs.writeScriptBin "lncli"
# Switch user because lnd makes datadir contents readable by user only
''
${runAsUser} lnd ${cfg.package}/bin/lncli \
${runAsUser} ${cfg.user} ${cfg.package}/bin/lncli \
--rpcserver ${cfg.rpcAddress}:${toString cfg.rpcPort} \
--tlscertpath '${secretsDir}/lnd-cert' \
--macaroonpath '${networkDir}/admin.macaroon' "$@"
Expand All @@ -139,6 +139,16 @@ in {
If left empty, no address is announced.
'';
};
user = mkOption {
type = types.str;
default = "lnd";
description = "The user as which to run LND.";
};
group = mkOption {
type = types.str;
default = cfg.user;
description = "The group as which to run LND.";
};
inherit (nbLib) enforceTor;
};

Expand All @@ -163,7 +173,7 @@ in {
environment.systemPackages = [ cfg.package (hiPrio cfg.cli) ];

systemd.tmpfiles.rules = [
"d '${cfg.dataDir}' 0770 lnd lnd - -"
"d '${cfg.dataDir}' 0770 ${cfg.user} ${cfg.group} - -"
];

systemd.services.lnd = {
Expand All @@ -183,7 +193,7 @@ in {
RuntimeDirectory = "lnd"; # Only used to store custom macaroons
RuntimeDirectoryMode = "711";
ExecStart = "${cfg.package}/bin/lnd --configfile=${cfg.dataDir}/lnd.conf";
User = "lnd";
User = cfg.user;
Restart = "on-failure";
RestartSec = "10s";
ReadWritePaths = cfg.dataDir;
Expand All @@ -206,7 +216,7 @@ in {
--cacert ${secretsDir}/lnd-cert \
-X GET ${restUrl}/genseed | ${pkgs.jq}/bin/jq -c '.cipher_seed_mnemonic' > "$mnemonic"
fi
chown lnd: "$mnemonic"
chown ${cfg.user}: "$mnemonic"
'')
(nbLib.script "lnd-create-wallet" ''
if [[ ! -f ${networkDir}/wallet.db ]]; then
Expand Down Expand Up @@ -263,21 +273,21 @@ in {
) // nbLib.allowAnyProtocol; # For ZMQ
};

users.users.lnd = {
group = "lnd";
users.users.${cfg.user} = {
group = cfg.group;
extraGroups = [ "bitcoinrpc" ];
home = cfg.dataDir; # lnd creates .lnd dir in HOME
};
users.groups.lnd = {};
users.groups.${cfg.group} = {};
nix-bitcoin.operator = {
groups = [ "lnd" ];
allowRunAsUsers = [ "lnd" ];
groups = [ cfg.group ];
allowRunAsUsers = [ cfg.user ];
};

nix-bitcoin.secrets = {
lnd-wallet-password.user = "lnd";
lnd-key.user = "lnd";
lnd-cert.user = "lnd";
lnd-wallet-password.user = cfg.user;
lnd-key.user = cfg.user;
lnd-cert.user = cfg.user;
lnd-cert.permissions = "0444"; # world readable
};
};
Expand Down
2 changes: 1 addition & 1 deletion modules/onion-addresses.nix
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ in {
${concatMapStrings (service: ''
onionFile=/var/lib/tor/onion/${service}/hostname
if [[ -e $onionFile ]]; then
install -o ${config.systemd.services.${service}.serviceConfig.User} -m 400 $onionFile ${service}
install -D -o ${config.systemd.services.${service}.serviceConfig.User} -m 400 $onionFile services/${service}
fi
'') cfg.services}
'';
Expand Down
2 changes: 1 addition & 1 deletion modules/onion-services.nix
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ in {
in srv.public && srv.enable
) services;
in genAttrs publicServices' (service: {
getPublicAddressCmd = "cat ${config.nix-bitcoin.onionAddresses.dataDir}/${service}";
getPublicAddressCmd = "cat ${config.nix-bitcoin.onionAddresses.dataDir}/services/${service}";
});
}

Expand Down
2 changes: 1 addition & 1 deletion modules/recurring-donations.nix
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ in {

users.users.recurring-donations = {
group = "recurring-donations";
extraGroups = [ "clightning" ];
extraGroups = [ config.services.clightning.group ];
};
users.groups.recurring-donations = {};
};
Expand Down
22 changes: 16 additions & 6 deletions modules/spark-wallet.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,42 @@ in {
encodes an URL for accessing the web interface.
'';
};
user = mkOption {
type = types.str;
default = "spark-wallet";
description = "The user as which to run spark-wallet.";
};
group = mkOption {
type = types.str;
default = cfg.user;
description = "The group as which to run spark-wallet.";
};
inherit (nbLib) enforceTor;
};

config = mkIf cfg.enable {
services.clightning.enable = true;

users.users.spark-wallet = {
group = "spark-wallet";
extraGroups = [ "clightning" ];
users.users.${cfg.user} = {
group = cfg.group;
extraGroups = [ config.services.clightning.group ];
};
users.groups.spark-wallet = {};
users.groups.${cfg.group} = {};

systemd.services.spark-wallet = {
wantedBy = [ "multi-user.target" ];
requires = [ "clightning.service" ];
after = [ "clightning.service" ];
script = startScript;
serviceConfig = nbLib.defaultHardening // {
User = "spark-wallet";
User = cfg.user;
Restart = "on-failure";
RestartSec = "10s";
} // (if cfg.enforceTor
then nbLib.allowTor
else nbLib.allowAnyIP)
// nbLib.nodejs;
};
nix-bitcoin.secrets.spark-wallet-login.user = "spark-wallet";
nix-bitcoin.secrets.spark-wallet-login.user = cfg.user;
};
}

0 comments on commit bcad047

Please sign in to comment.