Copy settings/config from User to Service

Hi, I’m beginner with nixos but I couldn’t find nice solution with examples online.
I’ve tried to accomplish copying my user config for one of nix package which would be executed as service from systemd. The plan was that once I login package could run as web service in the background on startup.

So inside configuration.nix I created my own service for systemd: = {
	description = "Some web service";

	serviceConfig = {
		ExecStartPre = ''
		/run/current-system/sw/bin/cp -a '/home/azelski/.config/SomePackage/config' . '${pkgs.SomePackage}/lib/SomePackage/config'
		ExecStart = "${pkgs.SomePackage}/bin/ExeFile";
		Restart = "always";

	wantedBy = [
  }; = true;

It does copy to /nix/store which feels wrong, and is from my knowledge impossible as it should be always read-only.
Running journalctl -u myService.service -b basically confirms it as I’m just getting bunch of Read-only file system errors.

My question is, how can I achieve what I initially want?
So my service will run under config from user. Can I do it in configuration.nix alone?
What is the workaround/correct way of doing it, or is it completely impossible?
Note: Running it by hand from my user with my user config does the trick, but I’d like to make it completely automated on start-up ideally inside configuration.nix so I could easily move all my stuff to different machine in future

I’d love and appreciate any suggestions/comments which could lead me to answer
Thank you!

Does your service have a way to specify a config file in its command line args?

I assume from the context of the request that making the config file a static part of your system, upgraded through nixos-rebuild, isn’t a workable solution.

Another option worth considering would be to define this service as a systemd user service through home-manager. Then the user can update it themselves.

1 Like

There’s a bunch of things wrong here.

  1. You should define this as a user service under instead.
  2. You should reference cp from coreutils, not the impure global path
  3. You are trying to copy to the store, which cannot work, so either:
    1. specify the path to the file if the daemon supports it, or
    2. patch the package to look for its config file somewhere else (under /nix/store is definitely wrong)
  4. To activate it, wantedBy = [ "default.service" ]
  5. You don’t have to enable the service. It is enabled by default unless explicitly disabled.

There is one thing to keep in mind though when you are doing this without home manager. A user level service defined this way is going to be enabled for all users. Now, if you’re the only person using this machine, you can of course just say “I don’t care”, but if you’re not, you need to do a little more work.

First of all, when passing the path to the config file, you should do something like %h/.config/SomePackage/config as systemd will then automatically substitute the path to $HOME. There are a bunch more you can see with man systemd.unit. No matter what, this is a healthy pattern rather than hardcoding user names/paths.

Secondly, with home-manager you would be able to define it just for your user.

But a workaround is to use unitConfig.ConditionUser = "azelski"; which will make it run just for you.

1 Like

My intention was to do it for all the users, so it wasn’t a blunder here. I was expecting hardcoding as awful solution but I didn’t knew any better as I wanted to make it first ‘work’ then think later how to improve it. Thanks for tip on environment paths, I will check it out.

@peterhoeg @tejing Thanks for all the response it’s super helpful, I’ll definitely look at home-manager, along with more glance of systemd in general.
I was aware that home manger exists before but the main reason I didn’t wanted to get into it was that I wanted to keep as much as possible in /etc/nixos/configuration.nix but I guess I’ll still have to hold .profile and other stuff somewhere so hope it will make it much better experience.
Anywas once again thanks for rapid and detailed replies!

I was aware that home manger exists before but the main reason I didn’t wanted to get into it was that I wanted to keep as much as possible in /etc/nixos/configuration.nix

You absolutely can keep the HM config in configuration.nix if you want.

1 Like