Skip to content

Commit

Permalink
Expose the fromCRD generator in default.nix
Browse files Browse the repository at this point in the history
  • Loading branch information
arnarg committed Oct 31, 2024
1 parent 62d70b7 commit c23f0df
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 42 deletions.
3 changes: 3 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ in {

# Have the nixidy cli available.
nixidy = pkgs.callPackage ./nixidy/nixidy.nix {};

# Have fromCRD generator available.
generators.fromCRD = (import ./pkgs/generators {inherit pkgs;}).fromCRD;
}
2 changes: 1 addition & 1 deletion docs/user_guide/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
pkgs = import nixpkgs {};

# Import nixidy
nixidy = import ../nixidy {inherit nixpkgs;};
nixidy = import sources.nixidy {inherit nixpkgs;};
in
pkgs.mkShellNoCC {
packages = with pkgs; [
Expand Down
120 changes: 79 additions & 41 deletions docs/user_guide/typed_resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,88 @@ The lack of availability of typed resource options only hinders the ability to d

Thankfully a code generator for generating resource options from CRDs is provided by nixidy (this is based heavily on kubenix's code generator).

As an example, to generate resource options for Cilium's `CiliumNetworkPolicy` and `CiliumClusterwideNetworkPolicy` the following can be defined in `flake.nix`.
=== "flakes"
As an example, to generate resource options for Cilium's `CiliumNetworkPolicy` and `CiliumClusterwideNetworkPolicy` the following can be defined in `flake.nix`.

```nix title="flake.nix"
{
description = "My ArgoCD configuration with nixidy.";
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.nixidy = {
url = "github:arnarg/nixidy";
inputs.nixpkgs.follows = "nixpkgs";
};
outputs = {
self,
nixpkgs,
flake-utils,
nixidy,
}: (flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {
inherit system;
};
in {
packages = {
generators.cilium = nixidy.packages.${system}.generators.fromCRD {
name = "cilium";
src = pkgs.fetchFromGitHub {
owner = "cilium";
repo = "cilium";
rev = "v1.15.6";
hash = "sha256-oC6pjtiS8HvqzzRQsE+2bm6JP7Y3cbupXxCKSvP6/kU=";
};
crds = [
"pkg/k8s/apis/cilium.io/client/crds/v2/ciliumnetworkpolicies.yaml"
"pkg/k8s/apis/cilium.io/client/crds/v2/ciliumclusterwidenetworkpolicies.yaml"
];
```nix title="flake.nix"
{
description = "My ArgoCD configuration with nixidy.";

inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
inputs.flake-utils.url = "github:numtide/flake-utils";

inputs.nixidy = {
url = "github:arnarg/nixidy";
inputs.nixpkgs.follows = "nixpkgs";
};
};
}));
}
```

Then running `nix build .#generators.cilium` will produce a nix file that can be copied into place in your repository. After that the generated file has to be added to `nixidy.resourceImports` in your nixidy modules.
outputs = {
self,
nixpkgs,
flake-utils,
nixidy,
}: (flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {
inherit system;
};
in {
packages = {
generators.cilium = nixidy.packages.${system}.generators.fromCRD {
name = "cilium";
src = pkgs.fetchFromGitHub {
owner = "cilium";
repo = "cilium";
rev = "v1.15.6";
hash = "sha256-oC6pjtiS8HvqzzRQsE+2bm6JP7Y3cbupXxCKSvP6/kU=";
};
crds = [
"pkg/k8s/apis/cilium.io/client/crds/v2/ciliumnetworkpolicies.yaml"
"pkg/k8s/apis/cilium.io/client/crds/v2/ciliumclusterwidenetworkpolicies.yaml"
];
};
};
}));
}
```

Then running `nix build .#generators.cilium` will produce a nix file that can be copied into place in your repository. After that the generated file has to be added to `nixidy.resourceImports` in your nixidy modules.

=== "flake-less"
As an example, to generate resource options for Cilium's `CiliumNetworkPolicy` and `CiliumClusterwideNetworkPolicy` the following can be defined in `generate.nix`.

```nix title="generate.nix"
let
# With npins
sources = import ./npins;
# With niv
# sources = import ./nix/sources.nix;

# nixpkgs added with:
# npins: `npins add --name nixpkgs channel nixos-unstable`
# niv: `niv add github nixos/nixpkgs -b nixos-unstable`
nixpkgs = sources.nixpkgs;
pkgs = import nixpkgs {};

# Import nixidy
nixidy = import sources.nixidy {inherit nixpkgs;};
in
{
cilium = nixidy.generators.fromCRD {
name = "cilium";
src = pkgs.fetchFromGitHub {
owner = "cilium";
repo = "cilium";
rev = "v1.15.6";
hash = "sha256-oC6pjtiS8HvqzzRQsE+2bm6JP7Y3cbupXxCKSvP6/kU=";
};
crds = [
"pkg/k8s/apis/cilium.io/client/crds/v2/ciliumnetworkpolicies.yaml"
"pkg/k8s/apis/cilium.io/client/crds/v2/ciliumclusterwidenetworkpolicies.yaml"
];
};
}
```


```nix title="env/dev.nix"
{
Expand Down

0 comments on commit c23f0df

Please sign in to comment.