0

On PCs, the XDG Desktop specifications allow the individual desktops to set different folders for various data. When writing an application that will only ever be run on servers, should one simply hardcode /etc/myapp, /var/cache/myapp etc, or are there potential environment variables or similar that should be checked?

1
  • Read man hier, the Linux Filesystem Hieracy document.
    – waltinator
    Dec 21, 2021 at 1:41

2 Answers 2

1

Most Linux applications that I know of indeed hardcode paths but sometimes allow to redefine them using environment variables and command line arguments.

I see nothing wrong with hardcoding these paths but maybe you'll make the lives of your potential app users easier if you allow to specify the configuration file location as a command line argument and in this conf file allow to change the location of /var/cache/myapp.

In case you hardcode everything at least make sure the existing packages in your distro don't conflict with your locations. For DNF based distros it will be (this is an example):

dnf whatprovides '/var/cache/dnf'
dnf-4.9.0-1.fc35.noarch : Package manager
Repo        : @System
Matched from:
Filename    : /var/cache/dnf

dnf-4.9.0-1.fc35.noarch : Package manager
Repo        : fedora
Matched from:
Filename    : /var/cache/dnf

So you obviously cannot use /var/cache/dnf.

In the end it's gonna much easier to just have everything installed in /opt/appname or /usr/local/opt/appname. No native Linux applications use these directories.

1

Should one hardcode paths on programs? NO!

In the least, you should make paths configurable at compile time, so the application can be installed under /var/local, or /opt, or /home/someuser, or where ever the system administrator wants. Better yet, have a command line option that can be used to point the program to its configuration file, which can then be used to set any other paths needed. (With default locations set at compile time.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .