Converting Docker-Compose files to Nix

I’m looking for an easy or automatic way to convert a docker-compose.yml file for usage in my server’s configuration.nix. I tried to translate the docker-compose file of invidious, but I fail at translating the environment variables. Is there a way to convert a docker-compose description into a declarative container config? Or can somebody maybe help me out with translating this file?

I’m not aware of any way of doing so automatically and that’s probably not something you’d want anyways.
I guess you could convert the YAML to JSON or TOML and then use the appropriate from function to get a Nix attrset which you could turn into an oci container options set with some recursive mapAttrs and filterAttrs but that sounds hairy.

As for environment variables, you need this option: NixOS Search - Loading...

The | in YAML functions similar to our ''; INVIDIOUS_CONFIG is just a literal string of YAML, declared inside docker-compose’s YAML.

In Nix it’d be:

 virtualisation.oci-containers.containers.<name>.environment = {
  INVIDIOUS_CONFIG = ''
    channel_threads: 1
    check_tables: true
    feed_threads: 1
    db:
      user: kemal
      password: kemal
      host: postgres
      port: 5432
      dbname: invidious
    full_refresh: false
    https_only: false
    domain:
  '';
}

If you want to get real fancy, you could even use lib.generators.toYAML to generate the config YAML from Nix attrsets:

virtualisation.oci-containers.containers.<name>.environment = {
  INVIDIOUS_CONFIG = lib.generators.toYAML {
    channel_threads = 1;
    check_tables = true;
    feed_threads = 1;
    db = {
      user = "kemal";
      password = "kemal":
      host = "postgres";
      port = 5432;
      dbname = "invidious";
    };
    full_refresh = false;
    https_only = false;
    domain = "";
  };
}

Welcome to Nix :wink:

(Note: None of the examples are tested; they may contain translation and/or syntax errors)

1 Like

Well, that solves my main problem. Thank you very much.