pkl icon indicating copy to clipboard operation
pkl copied to clipboard

Nix support

Open alexthotse opened this issue 1 year ago • 12 comments

Is there gonna be nix configuration support??

alexthotse avatar Feb 18 '24 16:02 alexthotse

Duplicate of #113

stackoverflow avatar Feb 19 '24 12:02 stackoverflow

Is it really a duplicate? What was the authors intention?

adrian-gierakowski avatar Mar 17 '24 09:03 adrian-gierakowski

@adrian-gierakowski nope not a duplicate. My intention was to use apple pkl to add support for nix configuration files such as configuration.nix and flakes to easily assist anyone looking to setup NixOS as a server or personal worksation. #113 speaks of installation of pkl on NixOS.

alexthotse avatar Mar 17 '24 13:03 alexthotse

Re-opened!

bioball avatar Mar 17 '24 15:03 bioball

Hi @stackoverflow , this is absolutely not a duplicate.

I've been thinking about & need this use-case as well @alexthotse . I think this is where pkl could truly shine for large scale adoption, in addition to all other strong use-cases/migrations like k8s workflows. I would have liked to move my ~/.config/home-manager/home.nix to ~/.config/home-manager/home.pkl as well.

pkl-cli eval command needs to support nix language:

$ jpkl eval -f nix home.pkl 

So far based on my investigation, it could provide that as long as eval-ed pkl objects don't have "entries" that are public(maybe "hidden" entries could pass the output render checks). Objects with pkl entries also fail to be converted to json or yaml. A json/yaml can always be converted to pkl and nix however pkl environment will miss the possible pkl type declarations(as it could have for home-manager) during eval runtime unless certain nix renderer is used.

Another option is to build a typed home-manager in pkl and interop will be challenging but earlier we start is better I think. Regardless a nix format is absolutely needed for $ pkl eval -f nix foo.pkl.

I'm still learning/investigating Pkl, I already see Pkl as an improvement to nix, the language overall(not the nix, the store).

izelnakri avatar Mar 19 '24 10:03 izelnakri

@izelnakri You clearly have a bigger picture as to how Nix and Pkl can coexist with one another. This is what I had envisioned in a nutshell. Thank you for wording it out.

@bioball Appreciate the re-open as this will help bring a new approach to future configuration of systems as a whole.

So the question would be how can we join our efforts to make this a possibility?

alexthotse avatar Mar 19 '24 19:03 alexthotse

Can a pkl file export a function?

adrian-gierakowski avatar Mar 19 '24 19:03 adrian-gierakowski

Can a pkl file export a function?

very good question @adrian-gierakowski & I made it sort of work :) However as far as I know the current language design discourages "one-function-per-file" concept unless its generated.

Pkl modules have a special output object that can have a value definition, this might look like an arbitrary choice but rather it seems to be the convention, internally speaking. Depending on the non-pkl output mode, the relevant parser/generator can/will always read this namespace. Example:

function.pkl:

output {
  value: (n) -> n * 2 // our custom function
}

main.pkl:

my_function = import("function.pkl") // if you want this key ignored by output make it "hidden my_function = import()"

result = my_function.output.value.apply(22)
jpkl eval -f json main.pkl
# json output will be { result: 44 }

izelnakri avatar Mar 19 '24 21:03 izelnakri

Functions are internal to Pkl; not exportable.

I don't have any experience working with nix. But: you can write an in-language renderer to turn Pkl into nix.

For example, here is a renderer that writes Pkl -> TOML: https://github.com/apple/pkl-pantry/blob/main/packages/pkl.toml/toml.pkl

bioball avatar Mar 19 '24 21:03 bioball

A nix output format is certainly a reasonable way in, although that could just be a renderer in Pkl (see the TOML renderer). That said, the nix format has a bunch of programming constructs that you'll not get out of Pkl. Doesn't nix tooling also read static formats anywhere? Alternatively, the real support would be got by getting nix to use Pkl as a library, so that it can 'natively' consume .pkl files.

(Edit: As @bioball said; I had choppy internet, so this posted late)

holzensp avatar Mar 20 '24 13:03 holzensp

Nix can import json (and toml) natively so if you generate, say k8s config in pkl you could import it into nix and manipulate further using nix language

but things like configuration.nix or home manager config are usually functions. Not sure how one could use pkl for that and what’s the benefit over just using nix

adrian-gierakowski avatar Mar 20 '24 18:03 adrian-gierakowski

but things like configuration.nix or home manager config are usually functions. Not sure how one could use pkl for that and what’s the benefit over just using nix

@adrian-gierakowski I think the benefit would be that nix, home-configuration.nix & configuration.nix could use Pkl documentation generator for documentation in the future and perhaps all the typed data structures would be easily debuggable, interactively peeked with $ jpkl repl. Both functions return an expected object and debugging/peeking that returned object before manipulation/development make things very productive including the benefit of being able to debug the imported flakes interactively.

Missing or impartial documentation for data structures has been my biggest DX issue with learning and doing anything advanced with nix/home-manager. I can see how Pkl could ease the learning curve of nix and development of advanced configurations and modules.

izelnakri avatar Mar 28 '24 21:03 izelnakri