Why does LoadFromPath() function of generated .pkl.go file needs .pkl file at runtime when there is already generated .pkl.go files. #128

Open
opened 2025-12-30 01:21:16 +01:00 by adam · 2 comments
Owner

Originally created by @hnrNeha on GitHub (Mar 28, 2024).

Iam using pkl files for config and generated go files for it. In the process of loading config Iam using LoadFromPath() this function expects a pkl file as parameter so for this I need to have pkl executable in my system.My query is when we are already generating go file from pkl why do we need to give pkl file path again.Is there any way to load pkl files from generated code without giving path to .pkl file??

Originally created by @hnrNeha on GitHub (Mar 28, 2024). Iam using pkl files for config and generated go files for it. In the process of loading config Iam using LoadFromPath() this function expects a pkl file as parameter so for this I need to have pkl executable in my system.My query is when we are already generating go file from pkl why do we need to give pkl file path again.Is there any way to load pkl files from generated code without giving path to .pkl file??
Author
Owner

@holzensp commented on GitHub (Mar 28, 2024):

The generated Go code is the structure of the configuration (the schema, or template). It does not contain the values of the configuration. Those, you load at runtime (so that you can change configuration out-of-sync with your application code).

@holzensp commented on GitHub (Mar 28, 2024): The generated Go code is the _structure_ of the configuration (the schema, or template). It does not contain the _values_ of the configuration. Those, you load at runtime (so that you can change configuration out-of-sync with your application code).
Author
Owner

@bioball commented on GitHub (Mar 28, 2024):

Phil is right about the generated code only being the structure, and not the values.

With that said, though, there are a couple options that you have to embed values from Pkl into your application.

Embed pkl files

One option is to use go:embed to embed those Pkl files as an embed.FS. You can then evaluate those modules straight from the embedded file system. Here is a commit that demonstrates that: 3b843c3966

The benefit of this approach is that because Pkl happens at runtime, you can pass in external variables into the Pkl program. For example, you can pass in environment variables and use read("env:") within Pkl to read those environment variables. This is especially helpful for things like reading in secret values into Pkl.

The drawback is that you still need to include the pkl CLI in the deployed environment of your application.

Pre-evaluate Pkl into binary

If you really don't want to include the pkl CLI at runtime, there's another option here, which is to first evaluate Pkl into a binary format, and then embed that binary into your application. Then, you can use pkl.Unmarshal to turn that binary into your app config.

That approach is demonstrated here: 841315bc14

@bioball commented on GitHub (Mar 28, 2024): Phil is right about the generated code only being the structure, and not the values. With that said, though, there are a couple options that you have to embed values from Pkl into your application. ## Embed pkl files One option is to use `go:embed` to embed those Pkl files as an `embed.FS`. You can then evaluate those modules straight from the embedded file system. Here is a commit that demonstrates that: https://github.com/bioball/pkl-go-examples/commit/3b843c396688f8034a872c8d490cab9f2a2f3880 The benefit of this approach is that because Pkl happens at runtime, you can pass in external variables into the Pkl program. For example, you can pass in environment variables and use `read("env:")` within Pkl to read those environment variables. This is especially helpful for things like reading in secret values into Pkl. The drawback is that you still need to include the `pkl` CLI in the deployed environment of your application. ## Pre-evaluate Pkl into binary If you really don't want to include the `pkl` CLI at runtime, there's another option here, which is to first evaluate Pkl into a binary format, and then embed that binary into your application. Then, you can use `pkl.Unmarshal` to turn that binary into your app config. That approach is demonstrated here: https://github.com/bioball/pkl-go-examples/commit/841315bc147ab892bc26fdc0c5d84ea547647a29
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#128