Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upConfig file management #7
Comments
spacekookie
changed the title
File Management
Config file management
Feb 20, 2018
killercup
added
the
tracking issue
label
Feb 20, 2018
This comment has been minimized.
This comment has been minimized.
|
The configure crate by @withoutboats abstract about configuration in general (as the name suggests), but doesn't seem to have an adapter for configuration files right now (except for Cargo.toml, but that's not the use case we have in mind I guess). |
This comment has been minimized.
This comment has been minimized.
|
I think it'd good to lay out some requirements for a crate(s) that would fill this gap:
I'd be OK with having multiple sub crates for various platforms, and then a "parent" crate that allows abstracting over the platform where all I do is specify a file name I want to load/save. |
This comment has been minimized.
This comment has been minimized.
|
Bonus if the crate can allow me to search custom directories, or tweak the order (on platforms where applicable). |
This comment has been minimized.
This comment has been minimized.
withoutboats
commented
Feb 20, 2018
Nope, the configure crate's default "source" is definitely designed for use cases where the person configuring the application is also the author - such as network services. However, the intent is for libraries to use configure, so that the application author can have total control over the source of configuration. A configuration source that integrates with configure and is designed for CLIs would be a great addition, and possibly one I'd be interested in upstreaming into configure proper. |
This comment has been minimized.
This comment has been minimized.
|
I'm thinking this issue might be part of a bigger topic.
It's probably uncommon for a single application to use all of these, but one or more should be common enough. It feels like these questions are part of the same problem; perhaps it might be useful to consider all of these questions as part of this discussion? |
This comment has been minimized.
This comment has been minimized.
|
For the question of temporary files there is already a crate which seems to do its job quite nicely (though I've only used it in limited scenarios so far, maybe it can be improved!) As for the rest…I think it would be pretty cool if we could create (or find and improve existing) crates that mirror the same behaviour for other configuration, essential and non-essential data files as well. It should be as simple as saying Edit Just as I hit "Comment" I found this crate here |
This comment has been minimized.
This comment has been minimized.
|
Hi, @soc! The Rust CLI working group is talking about cross-platform configuration file management and your directories crate has come up. Looking at your Github profile, I see you have a Java directories package as well, so you seem have some expertise in this area. Wanna chime in here? :) |
This comment has been minimized.
This comment has been minimized.
soc
commented
Feb 21, 2018
|
@killercup Sure, how can I help? |
This comment has been minimized.
This comment has been minimized.
|
@soc awesome! We were currently doing some research about the status quo of crates that are useful when writing CLI tools, work cross-platform and are maintained. For example, we want to come up with a good story around how to easily configure a CLI tool—with config files, env vars, and CLI flags. This issue is focussing on the handling of config files. @kbknapp already listed some good requirements in #7 (comment). Do you think directories is a good foundation here? What are your plans for it? Can we help you get it to 1.0? :) (@spacekookie and @yoshuawuyts probably have more to say!) |
This comment has been minimized.
This comment has been minimized.
soc
commented
Feb 22, 2018
•
directories is intentionally focused solely on dealing with operating system defaults. For instance, when dealing with CLI flags, the first issue you have is that of style (
I do think that directories is a good foundation for dealing with the operating system standards part of your goals. I believe that dealing with CLI flags should probably be done in a separate library, or in a way more specific to the individual application's needs, because dealing with CLI flags is very application-specific. In the end individual applications already need to have some custom code anyway to deal with migrating from storing their data directly in That's why directories only tells developers which directories they should be using, but does not get involved with creating directories itself, or making decisions about the priority of multiple directories (for instance platform defaults vs. CLI flags vs. config files). Application-specific code will be required to handle such issues, and I want directories to avoid getting involved in that: Often the cost of complexity to solve such issues in a general fashion in a library is way higher than dealing with it on the application side, especially when handling (legacy) applications with their own folder in Here is an example of an application that makes use of directories (the JVM version) and deals with migration compatibility, property files, and application-specific env vars: coursier/coursier#676.
My plan is to declare it as stable as fast as possible. I think the main blockers are
|
This comment has been minimized.
This comment has been minimized.
soc
commented
Feb 22, 2018
•
|
I have created tickets for the remaining issues I mentioned: soc/directories-rs#1 and soc/directories-rs#2. |
This comment has been minimized.
This comment has been minimized.
soc
commented
Feb 22, 2018
•
|
A more general note: There is a vast difference between selecting and standardizing on crates that provide certain functionality (like CLI parsing, config file parsing) and having one standardized way of handling application configuration: With the former you probably get crates that do almost everything and allow configuration of almost everything. With the latter, you want to be highly selective and make actual choices how things can be specified, and not allow a free for all in terms of decisions a developer can make. |
This comment has been minimized.
This comment has been minimized.
|
As you've noticed, I've opened some issues at directories-rs. I'd hold off on releasing a 1.0 before there are some consumers of the crate.
Absolutely. We already have some great libraries for CLI args, and I'd love to have an equally as good story for dealing with config files. That is not one crate – it's several build on top of and complementing each other :) (We'll hopefully see more concrete proposals for this in #6!) |
This comment has been minimized.
This comment has been minimized.
|
I think the focus should be less on a config file format and more on an API to get to those files. As a developer I might still want to be able to chose a format, say Not sure why you brought up CLI parsing. Although thinking about it now, I'm not sure how clap.rs handles windows arguments I haven't had a chance to play around with your crate yet but from the README it looks like it already exposes pretty much all the directory paths we might be interested in. At that point it becomes a question of making the API more ergonomic. i.e. maybe there could be a function to easily list configuration files for the given application (or |
This comment has been minimized.
This comment has been minimized.
I brought it up, sorry :) So, I've been thinking about what an all-around config solution might look like. We should not implement such a thing right now, but discuss what needs to happen to get there! Here's a small proposal that integrates ideas from clap (v3, this is future!) and configure to get the discussion going: #[derive(Debug, Deserialize, Clap, Configure)]
#[config(prefix = "diesel")]
struct Args {
#[clap(short = "q", long = "quiet")]
quiet: bool,
#[clap(long = "database-url")]
database_url: bool,
#[clap(subcommands)]
command: DieselCliCommand, // an enum defining subcommands with their own fields and attributes
}
fn main() {
let args = Args::configure()
.read_from(configure::adaptors::config_file::toml("diesel_cli.toml")) // Invokes serde
.read_from(configure::adaptors::env_file()) // dotenv
.read_from(configure::adaptors::env()) // std::env
.read_from(configure::adaptors::clap_auto_init()); // Clap incl. early exit on `-h` and stuff like that
}You can then:
Is that approximately the direction in which you want to go? What needs to happen to get there? |
This comment has been minimized.
This comment has been minimized.
TeXitoi
commented
Feb 22, 2018
|
I think the CLI/conf/env story should be in another issue. |
This comment has been minimized.
This comment has been minimized.
|
Sure, that was just for inspiration and to set some context. (If you have other use cases/ideas, please tell us :)) |
This comment has been minimized.
This comment has been minimized.
TeXitoi
commented
Feb 22, 2018
|
I have a couple of request in the structopt issues about that (no ideas, but persons wanting something like #7 (comment)) |
This comment has been minimized.
This comment has been minimized.
|
Like @spacekookie said, I think it should focus on abstracting over platform specific issues and not on the format, or providing "key->value" style API. As the application writer, I want to just specify a file name, and let this crate handle where to store it. I then worry about formats, reading/writing, etc. Then later on someone could write a generic crate to abstract over this configure crate, using something like serde to give a key->value style API. Here's how I see the crate structure playing out (note, the crate names are just generic and not referring to anything existing right now). |
This comment has been minimized.
This comment has been minimized.
Screwtapello
commented
Mar 2, 2018
|
At a former employer, I wrote a config file management library (in Python) that turned out to be popular with my fellow developers (because it was easy to add to an existing project) and with our operations staff (because all our tools worked the same way, and the configuration was flexible enough for most of our use-cases). It worked like this:
Pros:
Cons:
If I were to attempt something similar in Rust:
|
This comment has been minimized.
This comment has been minimized.
Screwtapello
commented
Mar 2, 2018
One reason to consider a standard config file format, or at least a standard config data model: on Windows, perhaps the standard configuration source could/should be the Registry, rather than the filesystem? |
This comment has been minimized.
This comment has been minimized.
soc
commented
Mar 2, 2018
|
After some research on that, it seems that most developers recommend and prefer files over the registry: |
This comment has been minimized.
This comment has been minimized.
derekdreery
commented
Mar 2, 2018
•
|
Since this thread is about the location of config files rather than their contents this may be a bit off topic, but here goes anyway: Similar to how structopt works, I'd love to do #[derive(Structconfig)]
pub struct Config {
timeout: u8
#[structconfig(name="retries", default=3)]
no_of_retries: u8,
files: Vec<PathBuf>,
}and have all the config stuff taken care of for me! Edit:
Didn't see that it had already been suggested. |
This comment has been minimized.
This comment has been minimized.
derekdreery
commented
Mar 2, 2018
|
How did your code deal with first run, if there wasn't a config file? Did it assume you wanted to use the defaults, or did it exit and prompt you to create a config file? (or did it walk you through creating the config file interactively?) |
This comment has been minimized.
This comment has been minimized.
Screwtapello
commented
Mar 2, 2018
|
At first run, it would use the defaults. For the various tools we created, every config option always had a sensible out-of-the-box default. Things the program absolutely could not know without asking would generally be command-line arguments, not config options. It's a big world, and I'm sure there's some potential config options that cannot possibly have a sensible default, but I can't think of one right now. If anyone has an example, I'd love to hear it. |
This comment has been minimized.
This comment has been minimized.
|
Another aspect of config management to consider is passwords. Looks like there is a |
This comment has been minimized.
This comment has been minimized.
kalefranz
commented
Mar 13, 2018
|
Hey everyone. I'm the current dev lead of conda, which is a cross-platform, system-level package manager. Currently written in python--but we're in the initial stages of considering transitioning key pieces to rust. Just wanted to add to this discussion how we do configuration, because it's been powerful and has worked out very well. It's also very similar to what @Screwtapello described. For each invocation of our executable, we build up a configuration context object from four sources of configuration information:
These are linearized in a way that the configuration sources conceptually closest to the process invocation take precedence. That is, if a configuration parameter is provided as a CLI flag, but also provided in a configuration file, the CLI-provided value would win. I guess the insight here is that most CLI applications deal with at least one configuration file, environment variables, and CLI flags anyway, and we've just realized that they all represent basically the same type of information, and can be generalized and unified. One capability that was especially important for us to add was the ability for sysadmins to lock down configuration for the entire system in "lower-level" read-only files. As we merge the sources of configuration information, we provide a flag sort of like the css I don't want to go into too much detail here. There's a blog post with more details, including how we deal with merging sequence and map-type configuration parameters. I did want to point all this out though as support for the usefulness of what @Screwtapello described. |
This comment has been minimized.
This comment has been minimized.
Screwtapello
commented
Mar 13, 2018
An alternative model that achieves the same goal is to have separate "config" and "override" files:
The advantage over an |
This comment has been minimized.
This comment has been minimized.
|
@i30817 is your concern with reading/writing a config file on the same system or sharing config files across systems? If sharing across systems, can you give more concrete details so we can flesh out what the requirements are for this? |
This comment has been minimized.
This comment has been minimized.
i30817
commented
Mar 23, 2018
•
|
sharing across systems. Common usecase is placing user readable/writable cfg files which contain relative paths to some dir (of a portable app or app that can be made portable like firefox, or dolphin or retroarch) in a flash drive. Supposedly users can use 'a/relative/path' syntax in windows. In practice, nearly all C and C++ programs/fileformat parsers blow up with this kind of relative path because they only ifdef _WIN32 '\' else '/' etc. There are complications with forbidden characters that are legal on one platform but not in another (unix to windows mostly). The vast majority of user made inputs to paths accepts both relative and absolute (when that is poison to moving systems). Which is kind of terrible, because the only way a user will get to have portable files is to use relative paths when possible but every cfg file and nearly all commands are determined to let the input be generalized to absolute paths. It would be nice if the cfg format generalized a 'relative to the cfg dir' path function/standard requiring a user readable relative path with '/', and returns a (current platform) normalized absolute to the cfg (optionally with another absolute parent dir overload?). In my mind this means early validation and early failure of corner cases and usecases that the user might have decided are 'necessary' (placing a absolute path, spaces without quotes, using a 'forbidden' character from OSes (besides '/' and closed quotes) on the 'relative path' etc). I wouldn't actually mind if there is also another clearly marked function that parses 'exotic' relative or absolute paths for the current running platform without this harsh early failure though (ie things like c:\home/relativejoin or a/relative), it should help to prevent the ifdef idiocy above. But that's basically the stdlib job and a library for cfg should promote the portable case. The only thing i'd regret is that this hack would also be useful for fileformats that insist on user readable paths like .cue for example, not only cfgs and might not be reinvented by several implementations like cue parsers in a attempt at 'generality'. But as long as PathBuf can deal with "a/relative with space" path on Windows, the very worst case is avoided for those formats because programmer laziness works for correctness here (unlike in C). But for cfg i believe you can do better, thus this request to make the 'default' path entries return a path relative to the cfg that is OS portable or fail hard (this could also be a constructor setting i guess). |
This comment has been minimized.
This comment has been minimized.
|
We have opened a "RFC-lite" on confy with reference to this. Do leave your feedback. |
This comment has been minimized.
This comment has been minimized.
sharkdp
commented
May 24, 2018
|
The |
This comment has been minimized.
This comment has been minimized.
soc
commented
May 24, 2018
|
@i30817 Interesting, I played with exactly this idea a few weeks ago:
|
This comment has been minimized.
This comment has been minimized.
i30817
commented
May 24, 2018
•
|
Yeah, well i reconsidered point 2 myself. I don't mind it being a warning during development, but i don't think it should be forced. Especially since most cli config paths are not controlled by devs but by the user and sometimes they themselves don't control what another tool/user named the files (imagine a file database that is using utf-8). Now i'm thinking that it should be a option to emit a warning about it, but not a 'kill the whole program' error. |
This comment has been minimized.
This comment has been minimized.
soc
commented
May 24, 2018
|
Yeah ... my approach comes with the expectation that developers are in control of their config file names. I think that's a reasonable assumption in 90% of the cases. In general, I think there is no having the cake and eating it: either you rule out the various OS' filesystem craziness, or you allow it and have the complexity go through the roof. I think there is value in having some crate that says "if you don't depend on the crazy parts of Windows path handling, this crate makes your life way easier". |
This comment has been minimized.
This comment has been minimized.
i30817
commented
May 24, 2018
•
|
It's true that is a concern more of other files that interact more with user paths that the typical config file (though 'last used recent path' in unix is a frequent exception), but i'm thinking that point 1 and 3 are sufficiently valuable that you want to give the ability for them to be used in those paths. |
This comment has been minimized.
This comment has been minimized.
soc
commented
May 24, 2018
•
|
@sharkdp It would be even more correct to use Apple's plist format if you write configuration to ~/Library/Preferences, but that's why the API avoids making any judgement on what you do with the paths directories gives you. (If I have some time in the future, I'd love to propose some RFC for Redox that overhauls config handling (using a @i30817 I pushed https://github.com/soc/paths-rs which is a rough sketch of my approach. |
This comment has been minimized.
This comment has been minimized.
pickfire
commented
Jul 10, 2018
|
https://crates.io/crates/dirs looks like a good choice for me even with it's limited features but the API is a lot simpler compared to https://crates.io/crates/directories. |
This comment has been minimized.
This comment has been minimized.
soc
commented
Jul 10, 2018
|
Yes, that's intentional, dirs just exposes individual functions, but if you want to derive paths for applications then you should absolutely use directories' |
This comment has been minimized.
This comment has been minimized.
kbd
commented
Nov 15, 2018
I would strongly expect any command line tool to use XDG standard location for config. There is no command line tool I know of that stores its config in Besides the lack of consistency in storing things in one location on Linux and another on Mac, if Rust utilities alone start storing things in |
This comment has been minimized.
This comment has been minimized.
soc
commented
Nov 16, 2018
•
|
@kbd As a bit of data: |
sharkdp
referenced this issue
Dec 7, 2018
Closed
Change the default configuration directory on macOS to .config/bat #442
lavifb
referenced this issue
Dec 7, 2018
Closed
Support for using ~/.config on Mac for command line apps #47
This comment has been minimized.
This comment has been minimized.
lavifb
commented
Dec 7, 2018
|
I would like to second @kbd. Command line tools are often cross-platform for *nix and as a result the config files are stored similarly to the way they do on Linux, at |
This comment has been minimized.
This comment has been minimized.
reitermarkus
commented
Dec 28, 2018
This comment has been minimized.
This comment has been minimized.
bitwalker
commented
Feb 4, 2019
|
Just wanted to chime in with my two cents here as well, this actually caught me off guard recently with a tool I was using, If the default directories end up in I basically never assume configs or other data associated with my shell environment are going to end up anywhere other than in the XDG base directories I have set, or in the worst case, my home directory - but I would never even think to look in For tooling I build, I certainly will be overriding this behaviour in |
This comment has been minimized.
This comment has been minimized.
soc
commented
Mar 31, 2019
•
|
I closed soc/directories-rs#47, please have a look at the explanation in soc/directories-rs#47 (comment) for details. In this case, In general, if I had a buck for every time I heard "I shouldn't need to follow the platform's standards because of X" I could have retired multiple times already. Stuff like this is way we can't have nice things. |
This comment has been minimized.
This comment has been minimized.
sharkdp
commented
Apr 4, 2019
•
|
@soc I understand that you do not want to include this in directories-rs, which probably also targets other (GUI) applications. However, as we have heard from multiple people in this thread (and multiple people in |
This comment has been minimized.
This comment has been minimized.
lavifb
commented
Apr 4, 2019
|
Also I'm not sure I understand the cache problem. Is it a big problem that cached files for CLI apps might be backed up? CLI caches do not tend to be that big in my experience. But even if it were a big problem, this is not a reason config files can't be placed in |
This comment has been minimized.
This comment has been minimized.
soc
commented
Apr 4, 2019
•
The difference between Linux and macOS is that Linux is a system written by developers, for developers. On the other hand., macOS is built by Apple, owned by Apple and controlled by Apple. The opinions of developers frankly doesn't matter on macOS – they are not calling the shots. If Apple ever decides to clamp down on applications not following their rules, I'm not going to take responsibility for the ensuing breakage. Just as a reminder,
is an excuse I heard more often than I care to count on Linux, too.
|
This comment has been minimized.
This comment has been minimized.
soc
commented
Apr 4, 2019
•
Pretty much every dependency manager on this planet is a CLI app. Have a look at the caches of Maven, npm or cargo. The cache directories are huge. |
This comment has been minimized.
This comment has been minimized.
lavifb
commented
Apr 4, 2019
But all of those store cache in the home dir on MacOS: More importantly, their config files are not buried somewhere deep in preferences but are all easily accessible in the home dir. |
This comment has been minimized.
This comment has been minimized.
soc
commented
Apr 4, 2019
|
Yes, I hope you now understand the issue with that (on both Linux and macOS). |
This comment has been minimized.
This comment has been minimized.
lavifb
commented
Apr 4, 2019
|
@soc
I do, however, think there is room for another crate that makes a different tradeoff here. |

spacekookie commentedFeb 20, 2018
•
edited by epage
(moderated summary by WG)
Context
Plan of Action
In-ecosystem resources
External inspiration
Challenges
@spacekookie's original poist
In the first meeting, we discussed the task of file management on different platforms (particularly configurations) and how it can be made better.
@Eijebong summarised it like this
There is a crate for "determing system configuration" (app-dirs-rs) but it seems unmaintained and not up to date