This project provides a framework for generating Nix Flakes from package recipes written in a Ruby DSL. This allows users to define how a package should be built using Ruby, which is then translated into a flake.nix file that Nix can use.
This project provides a Nix Flake (flake.nix at the root) to create a consistent development environment. This environment includes Ruby, which is necessary for running the generator and test scripts.
To activate the development environment:
-
Ensure you have Nix installed with Flakes enabled.
-
Navigate to the root of this project directory.
-
Run the following command to enter the development shell:
nix develop
Alternatively, you can use
nix shell .for a more ephemeral shell. -
Once inside the shell, you will have Ruby available. The environment also includes Minitest (for running tests) and is configured to support multiple platforms (Linux and macOS on x86_64 and ARM architectures).
Now you can proceed to use the generator scripts or run tests as described in the sections below.
recipes/: Contains the package definitions written in Ruby.- Example:
recipes/hello.rb
- Example:
lib/: Contains the Ruby DSL (recipe_dsl.rb) used in the recipes.generator/: Contains the script (generate_flake.rb) to convert Ruby recipes into Nix Flakes.flakes/: This directory is created by the generator. It stores the generated Nix Flakes and their associated source files.- Example:
flakes/hello/flake.nix - Example:
flakes/hello/src_files/*
- Example:
This section guides you through generating a Nix Flake for a simple "hello world" package.
Recipes are defined in Ruby. Here's the "hello world" example:
# recipes/hello.rb
package "hello" do
set_version "0.1.0" # Use set_version to define the package version
output do
# This block defines what the package will output/install.
# The following command creates a script file named 'hello' in a 'bin' subdirectory.
write_file "bin/hello", <<~SCRIPT
#!/bin/sh
echo "Hello, World from Nix Flake!"
SCRIPT
# This command marks the 'bin/hello' script as executable.
make_executable "bin/hello"
end
endDSL Methods:
package "name" do ... end: Defines a new package.set_version "version_string": Sets the package version.output do ... end: A block to define the build outputs.write_file "relative/path/to/file", "content": Writes content to a file that will be part of the package's source.make_executable "relative/path/to/file": Specifies that the given file should be made executable in the final package.
To generate the Nix Flake from a recipe:
-
Ensure you have Ruby installed.
-
Navigate to the root of this project directory.
-
Run the generator script, providing the path to the recipe:
ruby generator/generate_flake.rb recipes/hello.rb
-
This will produce the following output:
flakes/hello/src_files/bin/hello: The actual shell script.flakes/hello/flake.nix: The generated Nix Flake.
Once the flake.nix is generated, you would typically use Nix commands to build and run the package. For example:
# Navigate to the flake's directory
cd flakes/hello
# Build the package
nix build
# Run the executable (path may vary based on Nix version)
./result/bin/hello(Note: Actual Nix commands and usage are beyond the scope of this generator's README for now but are provided for context.)
This is an initial framework. Future enhancements could include:
- Dependency management.
- More complex build steps.
- Support for different types of sources (e.g., tarballs, Git repositories).
- Automated testing of generated Flakes.
This project uses Minitest for automated testing. The tests verify that the Nix Flake generator produces the expected output for the provided recipes.
ruby tests/test_hello_flake.rb