Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q: How can i make a module that creates a file without depending on systemd? #129646

Closed
Kreyren opened this issue Jul 8, 2021 · 10 comments
Closed
Labels
0.kind: question Requests for a specific question to be answered 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS

Comments

@Kreyren
Copy link
Contributor

Kreyren commented Jul 8, 2021

Context: I am trying to make a module to manage a zonefiles for https://git.dotya.ml/RiXotStudio/system-management/, i don't have experience with nix so doing babysteps to get there.

So far i've made: http://ix.io/3sih

@nh2
Copy link
Contributor

nh2 commented Jul 8, 2021

The question is vague an I am not sure if this is exactly what you are asking, but perhaps you would like to:

  • Create a file using e.g. environment.etc.<name>.text (see here in NixOS module option search)
  • Define its contents using the writeText function, see:
    /*
    * Writes a text file to nix store with no optional parameters available.
    *
    * Example:
    * # Writes contents of file to /nix/store/<store path>
    * writeText "my-file"
    * ''
    * Contents of File
    * '';
    *
    */
    writeText = name: text: writeTextFile {inherit name text;};
  • Use functions like map, concatStringsSep (from strings.nix) and so on to render out the contents of your zonefile (I'm assuming this is referring to a file as used like in the bind DNS server).

It's unclear to me whether that'll satisfy your requirement of "without depending on systemd", as I don't really know what that means. NixOS modules typically require running on NixOS, and NixOS uses systemd to boot. So while defining a file on the file system using environment.etc.<name>.text doesn't have a direct involvement of system, the fact that you're running NixOS will currently incur you a dependency on systemd running on that.

@Kreyren
Copy link
Contributor Author

Kreyren commented Jul 8, 2021

It's unclear to me whether that'll satisfy your requirement of "without depending on systemd", as I don't really know what that means. -- @nh2

I was given an example using systemd.tmpfiles.rules to make said file so i was looking for alternative that doesn't use systemd as i like to make my code init-independent where possible

(I'm assuming this is referring to a file as used like in the bind DNS server). -- @nh2

Yes, the end-goal is for bind, but i would like it flexible for other DNS servers in case e.g. unbound becames usable. (so with no hard dependency on bind)

Create a file using e.g. environment.etc..text (see here in NixOS module option search) -- @nh2

That seems to only create files in /etc ? I would like to make the file in /var/dns/thing.zone ideally

NixOS modules typically require running on NixOS, and NixOS uses systemd to boot. @nh2

Would like to make it flexible so that it could be used on NixOS forks e.g. NixNG which attempts to use OpenRC thus me wanting to use something else then systemd.tmpfiles

--

writeText and concatStringsSep are relevant, thank you! ^-^

--

@nh2
Copy link
Contributor

nh2 commented Jul 8, 2021

Would like to make it flexible so that it could be used on NixOS forks e.g. NixNG which attempts to use OpenRC thus me wanting to use something else then systemd.tmpfiles

I am not familar myself with OpenRC or NixNG, but I would imagine that it would probably be in NixNG's domain then to suggest some functionality how to procedurally generate files in not-immutable locations, like systemd.tmpfiles does for /var.

Given that Nix projects around integrating alternative init systems are reasonably young and new, I think there won't be an obvious generic way that's best for all of them yet.

However, I think you'll be on a good path of generating your desired zonefile content with writeText; it'll be easy to pass that generated text file to systemd.tempfiles, or some other mechanism provided by a different init system.

Also consider whether /var is really what you want. If you plan to decide to manage the zone file completely declaratively with nix, so that running programs would not mutate it, and it could be read-only, then environment.etc might actually be the cleaner approach.

@Kreyren
Copy link
Contributor Author

Kreyren commented Jul 8, 2021

Ideally i want it imutable by default stored in the nix-store and with mutable on demand stored in some mutable location. O.o

CC the maintainer of NixNG @MagicRB for opinion on the systemd-independendence

@MagicRB
Copy link
Contributor

MagicRB commented Jul 8, 2021

First off, if you're writing a module for NixOS, unless you basically want to stuff NixNG into NixOS, you'll have to depend on systemd. NixOS stands and falls on systemd. Also, yes you want to put it in /etc its not state, its generated with Nix and therefore should be either in /nix/store or /etc

@Kreyren
Copy link
Contributor Author

Kreyren commented Jul 8, 2021

@MagicRB i was thinking more like using writeText instead of systemd.tmpfiles.rules thinking that writeText is more friendly to other Inits or some handling to achive the file creation in a way that can be made compatible on NixNG and alike without changing my code

@MagicRB
Copy link
Contributor

MagicRB commented Jul 8, 2021

writeText just creates a file in the store, its just a derivation which echoes text into a file, literally

@roberth
Copy link
Member

roberth commented Jul 9, 2021

For a purely declarative file, the store or /etc is the way to do it.

If you want something imperative, you'll have to answer the question of "when" and that answer will very likely involve the init system. We don't have an abstraction for init systems at this time.

unless you basically want to stuff NixNG into NixOS

@MagicRB The module system supports patterns that would allow common modules to be shared between the two. Something along the lines of NixOS/rfcs#78 (comment), where pureModule is the shared module.

service abstraction

So splitting services into common and systemd modules is already a possibility and I have already done so to simplify maintenance between NixOS and nix-darwin, which is mostly just another init system too.
Similar logic can be applied to a module that declares a generic service, so you'll have the mystical but entirely feasible service abstraction layer.
When doing this, some experience writing modules is helpful, but I encourage anyone to play around with these ideas and tools.

@veprbl veprbl added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 0.kind: question Requests for a specific question to be answered labels Jul 10, 2021
@stale
Copy link

stale bot commented Jan 9, 2022

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jan 9, 2022
@abathur
Copy link
Member

abathur commented May 25, 2023

Closing since it sounds like the question was answered and this doesn't seem to be actionable, but I'm happy to reopen if I'm mistaken.

@abathur abathur closed this as completed May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: question Requests for a specific question to be answered 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS
Projects
None yet
Development

No branches or pull requests

6 participants