Simplifying Configuration with Pkl CLI on NixOS

Siva
2 min readFeb 11, 2024

--

Configuring applications and tools can sometimes be a complex task, especially in diverse environments. Pkl, a versatile CLI tool, aims to simplify configuration processes. In this blog post, we’ll guide you through the setup and utilization of Pkl on NixOS using a flake.

Getting Started

NixOS, known for its declarative configuration approach, provides an excellent foundation for managing development environments. We’ll harness the power of NixOS to seamlessly integrate Pkl into our setup.

Flake.nix Configuration

Our development environment will be defined in a flake.nix file. This snippet outlines the basic configuration:


# flake.nix

{
description = "Configuration CLI tool with Pkl on NixOS";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};

outputs = { self, nixpkgs }: {
devShell.x86_64-linux =
let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
jpklUrl = "https://repo1.maven.org/maven2/org/pkl-lang/pkl-cli-java/0.25.2/pkl-cli-java-0.25.2.jar";
jpklPath = "./jpkl";
in pkgs.mkShell {
buildInputs = [
pkgs.jdk # Add JDK as a dependency
];

shellHook = ''
# Download jpkl only if it doesn't exist
if [ ! -e ${jpklPath} ]; then
curl -L -o ${jpklPath} ${jpklUrl}
chmod +x ${jpklPath}
fi

export PATH="$PWD:$PATH" # Add the current directory to the PATH
echo "Entering the development environment!"
'';
};

devShells = rec {
default = self.devShell.x86_64-linux;
};
};
}

This configuration sets up a development environment with the Pkl CLI tool, ensuring it’s accessible in your current path during development.

Using Pkl for Configuration

Once inside the development environment, you can utilize Pkl to simplify configuration tasks. Explore Pkl commands tailored for configuration purposes:

# Evaluate Pkl configuration file
jpkl eval bird.pkl

# Evaluate Pkl configuration file and output in JSON format
jpkl eval -f json bird.pkl

# Evaluate Pkl configuration file and output in XML (plist) format
jpkl eval -f plist bird.pkl

Replace bird.pkl with your specific configuration file. The provided examples showcase different ways to evaluate the configuration file and output the results in JSON and XML formats.

Streamlining Your Workflow

Pkl’s simplicity and integration into the NixOS environment make it a powerful ally for configuration tasks. Experiment with Pkl commands, tweak configurations, and witness how it streamlines your workflow.

Conclusion

By combining the strengths of NixOS and the versatility of Pkl, configuring your development environment becomes a more straightforward process. Take advantage of the declarative approach offered by NixOS, and let Pkl enhance your configuration experience.

Happy configuring!.

--

--