Is it possible to use configuration.nix to write text to an arbitrary file?
Specifically I’m trying to write to /root/.gitconfig
:
[safe]
directory = /path/to/my/nixconfig
[user]
email = root@${hostname}
name = autoupgrade
so that auto-upgrade can update and commit flake.lock
: Flake auto-upgrade fails because git-repo not owned by current user
Previously when I needed to declare a specific file, I wrote an ad-hoc oneshot systemd service script to echo
the content and chmod
the file. But this feels very janky.
After some hours of searching and trying, the closest I have gotten is
environment.systemPackages = [
...
(pkgs.writeTextFile {
name = "gitconfig";
text = ''
[safe]
directory = /path/to/my/nixconfig
[user]
email = root@$hostname
name = autoupgrade
'';
destination = "/root/.gitconfig";
}
)
];
environment.pathsToLink = [ "/root" ] ;
which creates the file in the in the nix-store , and symlinks to the nix-store file from /run/current-system/sw/root/.gitignore
, but still nothing in /root/.gitignore
. I thought nixos-rebuild
would also place a symlink in /root/
There is etc.environment
for files in /etc
and home-managers home.file
for files in $HOME
, and various other specialised options, but I haven’t found a generic way to write to any arbitrary file. I’m very new to nix, so maybe I’m not using the search term
Is there a proper way to write to arbitrary files?