NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.09k stars 14.08k forks source link

nixos/klipper: configuration section sorting problem #185881

Open sorki opened 2 years ago

sorki commented 2 years ago

This is a minor issue I've discovered recently -

I'm unable to configure additional thermistor and sensor using it via services.klipper.settings because klipper expects [thermistor ...] section to come before [temperature_sensor ...] section.

This seems to be caused by Nix settings -> INI output which sorts the sections based on their name and after skimming the output format implementation I didn't see a way to disable sorting (not sure if it's a Nix thing or INI output thingie).

Ideally the output would match the order in which settings are defined in Nix.

Snippet with a workaround from my configuration:

let   
  ntc10k = pkgs.writeText "ntc10k.cfg"
  ''
  [thermistor ntc10k]
  temperature1: 25
  resistance1: 10000
  beta: 3977
  '';

<snip>

  /*  
  # XXX: we can't list this here
  # since klipper expects the definition
  # to be present in config before
  # temperature block which uses it
  # but the config sections get sorted
  # on export
  "thermistor ntc10k" = {
    temperature1 = 25;
    resistance1 = 10000;
    beta = 3977; # NTC10k brown black orange
  };
  # so we hack around using include.. which
  # comes before [temperature_sensor ..] block
  */
  "include /etc/klipper_thermistor_ntc10.cfg" = {}; 

  "temperature_sensor chamber" = { 
    sensor_type = "ntc10k";
    sensor_pin = "PF1";
  }; 

CC @lovesegfault @vtuan10

vtuan10 commented 2 years ago

This is difficult to achieve in Nix, as it does not preserve the order of attribute sets internally.

See also: https://github.com/NixOS/nixpkgs/issues/81986

I'm not sure how to fix this on the Nix side, as these would look uglier than your workaround. Does it make sense to bring this upstream, so that klipper's parser does not assume any ordering?

sorki commented 2 years ago

I see. I'm fine with the workaround but I'll create an upstream issue if I can't fix it easily.

lovesegfault commented 2 years ago

A possible solution is making the argument a list of attribute sets.