Big systemd user service trouble

hello grand wizards of NixOS

im trying to bring this systemd service to nix:

[Unit]
Description=autoadb

[Service]
Type=simple
Environment=ADB="full_path_to_adb" #like "/home/$USER/Android/Sdk/platform-tools/adb"
ExecStart=/usr/local/bin/autoadb scrcpy -s '{}'

[Install]
WantedBy=default.target

found here scrcpy with autoadb | Engineering @ Logic Soft

what I came up so far:

  systemd.user.services.scrcpy = {
    enable = true;
    description= "scrcpy";
    wantedBy = [ "graphical-session.target" ];
    serviceConfig = {
      ExecStart = "${pkgs.autoadb}/bin/autoadb scrcpy";
      Environment= "ADB=\"/home/usr/.android/adb.5037\"";
    };
  };

sadly I don’t get it to work

↪ systemctl --user status scrcpy.service
â—Ź scrcpy.service - scrcpy
     Loaded: loaded (/etc/systemd/user/scrcpy.service; enabled; preset: enabled)
     Active: active (running) since Sun 2024-10-27 19:16:32 CET; 17min ago
   Main PID: 5497 (autoadb)
      Tasks: 1 (limit: 18819)
     Memory: 732.0K (peak: 1.3M)
        CPU: 212ms
     CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/scrcpy.service
             └─5497 /nix/store/prhzdrw2652qc4a2s9vjrclps0h7jfg0-autoadb-unstable-2020-06-01/bin/autoadb scrcpy

Okt 27 19:16:32 dellxps systemd[1599]: Started scrcpy.

the service looks good to me, when I connect my phone it won’t pop up on the screen.

when doing the command directly in terminal it’s working:

↪ autoadb scrcpy
* daemon not running; starting now at tcp:5037
* daemon started successfully
Detected device 09121FDD4001R4
scrcpy 2.4 <https://github.com/Genymobile/scrcpy>
INFO: ADB device found:
INFO:     -->   (usb)  09121FDD4001R4                  device  Pixel_5
/nix/store/c4aggysjy41bnr1psy9bgyaxj5yn3fr5-scrcpy-2.4/share/scrcpy/scrcpy-server: 1 file pushed, 0 skipped. 35.5 MB/s (69007 bytes in 0.002s)
[server] INFO: Device: [Google] google Pixel 5 (Android 14)
INFO: Renderer: software
INFO: Texture: 1080x2336

would be sooo nice to have the screen of my phone pop up and interact with my phone on my pc every time I connect it.

thank you for your answer in advance!

I would first try to do exactly what the tutorial you linked to is doing and get that working. Because even on NixOS, you can still create user systemd units under .config (I am pretty sure atleast). Then I would also inspect so that the generated unit file looks good with systemctl cat.

2 Likes

systemd.user.services does not create unit files under the users .config, but instead creates global services in `/etc/systemd/user´ which get run for each user on the system. So there might be some subtle difference there.

1 Like

thank you crazyminecuber, will try that …

ok I did set up a systemd manually in .config/systemd/user:

[Unit]
Description=autoadb

[Service]
Type=simple
Environment=ADB="/home/usr/.android/adb.5037"
ExecStart=/run/current-system/sw/bin/autoadb scrcpy -s '{}'

[Install]
WantedBy=default.target

now I get this error message:

↪ journalctl --user-unit=scrcpy.service -f
Okt 27 20:17:18 dellxps autoadb[5490]: ERROR: Could not start adb server
Okt 27 20:17:18 dellxps autoadb[5490]: ERROR: Server connection failed
Okt 27 20:17:18 dellxps autoadb[5490]: scrcpy 2.4 <https://github.com/Genymobile/scrcpy>
Okt 27 20:17:30 dellxps autoadb[5390]: Detected device 09121FDD4001R4
Okt 27 20:17:30 dellxps autoadb[5556]: exec: Permission denied
Okt 27 20:17:30 dellxps autoadb[5553]: ERROR: Failed to execute: [/home/usr/.android/adb.5037], [start-server]
Okt 27 20:17:30 dellxps autoadb[5553]: ERROR: Could not execute "adb start-server"
Okt 27 20:17:30 dellxps autoadb[5553]: ERROR: Could not start adb server
Okt 27 20:17:30 dellxps autoadb[5553]: ERROR: Server connection failed
Okt 27 20:17:30 dellxps autoadb[5553]: scrcpy 2.4 <https://github.com/Genymobile/scrcpy>

I think we are getting closer …

[Unit]
Description=autoadb

[Service]
Type=simple
ExecStart=/run/current-system/sw/bin/autoadb scrcpy

[Install]
WantedBy=default.target

now this is working!
I removed the line Environment=ADB="/home/usr/.android/adb.5037"

That part shouldn’t have caused any issues in this case, by the way, so you can probably just switch back to doing it with your NixOS config.

You may want to switch to creating the service with home-manager if you use that, to prevent any other users from auto-launching this, but this will only apply to non-system users, so it shouldn’t do any harm in most cases.

Removing the env var presumably works because you install adb with NixOS as well? You could try ADB=${pkgs.android-tools}/some/subdir/adb instead.

1 Like

You may want to switch to creating the service with home-manager if you use that

yes this may be the smarter choice!

Removing the env var presumably works because you install adb with NixOS as well?

yes installed with NixOS

thank you TLATER I see you around here so much helping out noopy noops like me! I hope I can reach your expertise and understanding one day …

1 Like

Yep, fair enough. So to explain why your initial attempt didn’t work - the ADB variable clearly needs to point to your system’s adb binary, which scrcpy will use to know where adb is.

/home/usr/.android/adb.5037 is almost certainly not an adb binary - it looks more like a PID-file (hence the 5037 suffix, which is probably the PID of the adb process at some point in the past), which is used to track that there is a running adb server, and can be used to prevent launching multiple or help killing a server that has gone haywire. These files are usually empty, but sometimes contain some metadata.

So with your original systemd unit scrcpy was trying to execute a random text file, which obviously didn’t work.

Obviously scrcpy is also just clever enough to use the adb from your $PATH, but the tutorial you followed assumes you have android-tools installed in a prefix, presumably following on from an earlier tutorial from their site or the official android-tools installation instructions. This does not apply to distro packages, but guides like giving non-distro-specific instructions because that is more generic, and gives you more up-to-date installations on the distros that update more slowly (though in practice it typically still only works on debian/ubuntu).

If you want to, you can still point at the correct adb binary for NixOS, which is somewhere in the android-tools package, but it’s pretty pointless since you still need to have programs.adb.enable set to get the correct udev rules installed as well.

2 Likes