Systemd service fails with invalid env variable

Hello, im trying to make a simple systemd service to autosync my nextcloud. However the process fails with Invalid environment variable name evaluates to an empty string: (cat

It seems to more an issue about my lack of unix understanding as an issues with nixos itself. im relatively new and thus sorry to bother with this kind of problem.

when i run the command manually with my user (fschn) it works.

How can i ensure the process has access to the deployed secrets/credentials? or is in the right environment…

really appreciate any hint into the right direction. thank you!

nix config:

  sops.secrets."Nextcloud/my-user/user" = {
    owner = config.users.users.fschn.name;
    mode = "0400";
  };
  sops.secrets."Nextcloud/my-user/password" = {
    owner = config.users.users.fschn.name;
    mode = "0400";
  };

  environment.systemPackages = with pkgs; [
    nextcloud-client
  ];

  systemd.services.nextcloud-autosync = {
    unitConfig = {
      Description = "Auto sync Nextcloud";
      After = "network-online.target";
    };
    script = "${pkgs.nextcloud-client}/bin/nextcloudcmd -h --user $(cat ${config.sops.secrets."Nextcloud/my-user/user".path}) --password $(cat ${config.sops.secrets."Nextcloud/
      ▍      ↪ my-user/password".path}) --path /InstantUpload/OpenCamera '/tank/Photos/Phone Pixel 8A/OpenCamera' https://cloud.fschn.org";
    serviceConfig = {
   User = config.users.users.fschn.name
    };
    wantedBy = [ "multi-user.target" ];
    startAt = "*-*-* *:00:00";

  };

full error log:

$ journalctl -xeu nextcloud-autosync
Feb 13 12:00:00 omhe systemd[1]: Started Auto sync Nextcloud.
â–‘â–‘ Subject: A start job for unit nextcloud-autosync.service has finished successfully
â–‘â–‘ Defined-By: systemd
â–‘â–‘ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
â–‘â–‘
â–‘â–‘ A start job for unit nextcloud-autosync.service has finished successfully.
â–‘â–‘
â–‘â–‘ The job identifier is 2912762.
Feb 13 12:00:00 omhe (cloudcmd)[2590260]: nextcloud-autosync.service: Invalid environment variable name evaluates to an empty string: (cat
Feb 13 12:00:00 omhe nextcloudcmd[2590260]: 02-13 12:00:00:633 [ info nextcloud.sync.accessmanager ]:        2 "" "https://cloud.fschn.org/status.php" has X-Request-ID "2dd8abbf-777e-4e3d-9cfe-bdd6bb9a36df"
Feb 13 12:00:00 omhe nextcloudcmd[2590260]: 02-13 12:00:00:647 [ info nextcloud.sync.networkjob ]:        OCC::CheckServerJob created for "https://cloud.fschn.org" + "status.php" ""
Feb 13 12:00:00 omhe nextcloudcmd[2590260]: 02-13 12:00:00:696 [ info nextcloud.sync.networkjob.checkserver ]:        status.php returns:  QJsonDocument({"edition":"","extendedSupport":false,"installed":true,"maintenance":false,"needsDbUpgrade":false,"productname":"Nextcloud","version":"30.0.5.1","versionstring":"30.0.5"})   QNetworkReply::NoError  Reply:  QNetworkReplyHttpImpl(0x560c611e1c40)
Feb 13 12:00:00 omhe nextcloudcmd[2590260]: 02-13 12:00:00:696 [ info nextcloud.sync.accessmanager ]:        2 "" "https://cloud.fschn.org/ocs/v1.php/cloud/capabilities?format=json" has X-Request-ID "78240d84-692c-4ab2-8466-dffc01ccde5a"
Feb 13 12:00:00 omhe nextcloudcmd[2590260]: 02-13 12:00:00:696 [ info nextcloud.sync.networkjob ]:        OCC::JsonApiJob created for "https://cloud.fschn.org" + "ocs/v1.php/cloud/capabilities" ""
Feb 13 12:00:25 omhe nextcloudcmd[2590260]: 02-13 12:00:25:759 [ warning nextcloud.sync.credentials.http ]:        Stop request: Authentication failed for  "https://cloud.fschn.org/ocs/v1.php/cloud/capabilities?format=json"
Feb 13 12:00:25 omhe nextcloudcmd[2590260]: 02-13 12:00:25:760 [ warning nextcloud.sync.networkjob ]:        QNetworkReply::AuthenticationRequiredError "Host requires authentication" QVariant(int, 401)
Feb 13 12:00:25 omhe nextcloudcmd[2590260]: 02-13 12:00:25:761 [ info nextcloud.sync.networkjob.jsonapi ]:        JsonApiJob of QUrl("https://cloud.fschn.org/ocs/v1.php/cloud/capabilities?format=json") FINISHED WITH STATUS "AuthenticationRequiredError Host requires authentication"
Feb 13 12:00:25 omhe nextcloudcmd[2590260]: 02-13 12:00:25:761 [ warning nextcloud.sync.networkjob.jsonapi ]:        Network error:  "ocs/v1.php/cloud/capabilities" "Host requires authentication" QVariant(int, 401)
Feb 13 12:00:25 omhe nextcloudcmd[2590260]: 02-13 12:00:25:761 [ debug default ]        [ main(int, char**)::<lambda(const QJsonDocument&)> ]:        Server capabilities QJsonObject()
Feb 13 12:00:25 omhe nextcloudcmd[2590260]: 02-13 12:00:25:764 [ warning nextcloud.sync.configfile ]:        Enforcing default update channel "stable"
Feb 13 12:00:25 omhe nextcloudcmd[2590260]: Error connecting to server
Feb 13 12:00:25 omhe systemd[1]: nextcloud-autosync.service: Main process exited, code=exited, status=1/FAILURE
â–‘â–‘ Subject: Unit process exited
â–‘â–‘ Defined-By: systemd
â–‘ â–‘ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
â–‘â–‘
â–‘â–‘ An ExecStart= process belonging to unit nextcloud-autosync.service has exited.
â–‘â–‘
â–‘â–‘ The process' exit code is 'exited' and its exit status is 1.
Feb 13 12:00:25 omhe systemd[1]: nextcloud-autosync.service: Failed with result 'exit-code'.
â–‘â–‘ Subject: Unit failed
â–‘â–‘ Defined-By: systemd
â–‘â–‘ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
â–‘â–‘
â–‘â–‘ The unit nextcloud-autosync.service has entered the 'failed' state with result 'exit-code'.
Feb 13 12:00:25 omhe systemd[1]: nextcloud-autosync.service: Consumed 230ms CPU time, 21.1M memory peak, 4K read from disk, 5.6K incoming IP traffic, 2.3K outgoing IP traffic.
â–‘â–‘ Subject: Resources consumed by unit runtime
â–‘â–‘ Defined-By: systemd
â–‘â–‘ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
â–‘â–‘
â–‘â–‘ The unit nextcloud-autosync.service completed and consumed the indicated resources.

the manually run command:
/nix/store/15pzvamgk7q7j57s2kx2a6kq4sjy4rdw-nextcloud-client-3.14.4/bin/nextcloudcmd -h --user $(cat /run/secrets/Nextcloud/my-user/user) --password $(cat /run/secrets/Nextcloud/my-user/password) --path /InstantUpload/OpenCamera '/tank/Photos/Phone Pixel 8A/OpenCamera' https://cloud.fschn.org

It’s not doing shell substitution here, it’s literally treating $(cat as an envvar and then of course failing to find it.

To make your life easier, does nextcloudcmd not accept a path or some envvar to read account info from?

I don’t understand why though. OP is using script, which means that command is being run by a bash interpreter, so it should do shell substitution.

Yeah, it’s odd.

By the way, I hope your code doesn’t literally have these characters in it?

Also, what’s the output of systemctl cat nextcloud-autosync?
And ExecStart should point to a script, what are the contents of that script?

I embarrassingly need to admit that they literally were… I’m very sorry! removed them and now everything works. Really appreciate your time and effort. and my apologies again!

1 Like

Happens to the best of us. Cheers!