(Sorry in advance for the long intro)
You know how flatpak is kind of annoying to run from the terminal? Usually this is solved with symbolic links or simple scripts, but using NixOS you are encouraged to use a declarative approach, with idempotent scripts without secondary effects. For this I’ve made the following script:
Inside /etc/nixos/configuration.nix in the let in block:
fp_bin=/tmp/flatpak/bin
if [ -d "$fp_bin" ] # So it doesn't fail during rebuild
then rm "$fp_bin"/*
else mkdir -p "$fp_bin"
fi
appname() {
local desktop_file="$1"
${pkgs.gawk}/bin/awk -F= '$1=="Name" {
name=$2
name=tolower(name)
gsub(/ /,"_",name)
print name
exit
}' "$desktop_file"
}
app_id() {
local filename="$(basename "$1")"
echo "''${filename%.desktop}"
}
shopt -s nullglob
for dfile in /var/lib/flatpak/exports/share/applications/*.desktop; do
app_id="$(app_id "$dfile")"
appname="$(appname "$dfile")"
ln -s "/var/lib/flatpak/exports/bin/$app_id" \
"$fp_bin/$appname"
done
and used it in:
systemd.services.flatpak_bin = {
wantedBy = [ "multi-user.target" ];
path = [ pkgs.flatpak pkgs.gawk ];
script = flatpak_bin; #The script defined in the `let in` block
serviceConfig.Type = "oneshot";
};
I add /tmp/flatpak/bin to PATH in bash_profile, and the problem is solved. The script runs at startup. It is annoying that it doesn’t add things at the moment you do flatpak install <app>, but it works. And since it’s on a /tmp folder, once you shut down it disappears, without secondary effects.
(And here begins the point of the post)
But I see two problems with this approach:
- First, it doesn’t seem very “Nixy”. I’m quite new with NixOS, and I feel like I might be reinventing the wheel in a bad way, and barely using the nix language itself.
- Second, and more importantly, I get that the idea is that if I remove the systemd unit from the config file, any file produced should be deleted, and this only works IF I can use the
/tmpdirectory, and that isn’t be always the case.
Am I approaching this kind of problems in a bad way? I’m not looking for a solution for a specific problem, but general tips manage these problems better (although it could be a good example to show some things). Thanks a lot to everybody!