How do we refer to etc package from NixOS configuration?

2.2k Views Asked by At

I want to get a path, which leads to nixos /etc location (any one of /run/current-system/etc or /nix/store/hashhere-etc-1.0). I use this path to configure pppd connect script, some kind of the following,

  environment.etc."huawei" =
    { text = ''
        /dev/ttyUSB0
        38400
        lock
        crtscts
        nodetach
        noipdefault
        # Below here what I've struggled
        connect ${pkgs.etc}/${environment.etc."huawei-script".target}
      '';
      mode = "0777";
      target = "ppp/peers/huawei"; };

I have tried to write ${pkgs.etc} or ${system.build.etc} or even ${environment.etc} resulting errors.

The directory structure is actually relative, but I think it's safer to use absolute path.

    /nix/store/...etc.../ppp/peers
    |- huawei
    |- huawei.d
       |- huawei.sh
       |- huawei.chat 
3

There are 3 best solutions below

4
brocking On BEST ANSWER

If I understand correctly your problem is you simply need to pass the string value of the target attribute to the huawei.text connect directive. As per the description for the target attribute the value is a path relative to /etc so you should be able to either:

  1. Make the value of the connect directive the string literal connect /etc/ppp/peers/huawei or
  2. make the etc.huaweiattribute set a recursive one so that the attributes can refer to each other then do

    environment.etc.huawei = rec {
        target = "ppp/peers/huawei";
        text = ''...
                 # Below here what I've struggled
                 connect ${target}
        '';
    };
    
3
danbst On

You can refer to path to file in /nix/store/...etc... like this:

{ config, pkgs, lib, ... }:

{
  environment.etc."test".text = "helo";
  environment.etc."test2".text = "${config.environment.etc."test".source.outPath}";
}

Now I have in /etc/test2:

$ cat /etc/test2
/nix/store/1igc2rf011jmrr3cprsgbdp3hhm5d4l0-etc-test
0
Abdillah On

Sorry, I was overlook a fact where NixOS actually map any files in /nix/store/...etc../ into the /etc itself.

So, to refer to a file, it is better to use /etc directly.

connect /etc/${environment.etc."huawei-script".target}