Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add replace xyzBuilder with unified 'outputsBuilder' #59

Merged
merged 4 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:

# Execute /tests/*
- run: nix develop --command check-derivation-outputs
- run: nix develop --command check-derivation-outputs-old
- run: nix develop --command check-channel-patching
- run: nix develop --command check-overlays-flow
- run: nix develop --command check-hosts-config
Expand Down
5 changes: 5 additions & 0 deletions devShell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ devshell.mkShell {

(test "channel-patching")
(test "derivation-outputs")
(test "derivation-outputs-old")
(test "hosts-config")
(test "overlays-flow")
(test "all" // { command = "check-channel-patching && check-derivation-outputs && check-derivation-outputs-old && check-hosts-config && check-overlays-flow"; })

(dry-nixos-build "minimal-multichannel" "Hostname1")
(dry-nixos-build "minimal-multichannel" "Hostname2")
(dry-nixos-build "home-manager+nur+neovim" "Rick")
(dry-nixos-build "exporters" "Morty")
(withCategory "dry-build" { name = "build-all"; command = "build-exporters-Morty && build-home-manager+nur+neovim-Rick && build-minimal-multichannel-Hostname1 && build-minimal-multichannel-Hostname2"; })

];

}
6 changes: 4 additions & 2 deletions examples/exporters/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
inherit (self) pkgs inputs;
};

# construct packagesBuilder to export all packages defined in overlays
#packagesBuilder = fromOverlays self.overlays;
outputsBuilder = channels: {
# construct packagesBuilder to export all packages defined in overlays
packages = fromOverlays self.overlays channels;
};

overlay = import ./overlays;

Expand Down
34 changes: 25 additions & 9 deletions src/systemFlake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
extraArgs = sharedExtraArgs;
}

, outputsBuilder ? null

, packagesBuilder ? null
, defaultPackageBuilder ? null
, appsBuilder ? null
Expand Down Expand Up @@ -69,6 +71,7 @@ let
"sharedOverlays"
"supportedSystems"

"outputsBuilder"
"packagesBuilder"
"defaultPackageBuilder"
"appsBuilder"
Expand Down Expand Up @@ -189,24 +192,37 @@ mergeAny otherArguments (

pkgs = mapAttrs importChannel channels;

mkOutput = output: builder:

deprecatedBuilders = channels: { }
// optionalAttrs (packagesBuilder != null) { packages = packagesBuilder channels; }
// optionalAttrs (defaultPackageBuilder != null) { defaultPackage = defaultPackageBuilder channels; }
// optionalAttrs (appsBuilder != null) { apps = appsBuilder channels; }
// optionalAttrs (defaultAppBuilder != null) { defaultApp = defaultAppBuilder channels; }
// optionalAttrs (devShellBuilder != null) { devShell = devShellBuilder channels; }
// optionalAttrs (checksBuilder != null) { checks = checksBuilder channels; };

systemOutputs = (if outputsBuilder == null then deprecatedBuilders else outputsBuilder) pkgs;

mkOutput = output:
mergeAny
# prevent override of nested outputs in otherArguments
(optionalAttrs (otherArguments ? ${output}.${system})
{ ${output} = otherArguments.${output}.${system}; })
(optionalAttrs (args ? ${builder})
{ ${output} = args.${builder} pkgs; });
(optionalAttrs (systemOutputs ? ${output})
{ ${output} = systemOutputs.${output}; });

Comment on lines -197 to +213
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be better done with a mapAttrs. That way its future proofed if more system-spaced outputs ever get added.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also code is a bit simpler, mkOutput is no longer necessary

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree mapAttrs would be better option but we are dealing with multiple restrictions:

  • nix flake show should not error - we cant output empty attrsets
  • outputs must be merged - if we just go with mapAttrs while keeping in mind the first point - not all attrs will get merged.

And yeah, I guess I could go over the names and just generate attrset out of that. Probably will change that but I really should go and get some sleep now :D

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, I think it could probably be used in the future once we remove the deprecated arguments. For now this works well.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not going to do the attrs from list thingy. At least for time being - could not find a solution that I'd be happy with. Its other solutions are more complex and use around the same amount of lines

in
{ inherit pkgs; }
// mkOutput "packages" "packagesBuilder"
// mkOutput "defaultPackage" "defaultPackageBuilder"
// mkOutput "apps" "appsBuilder"
// mkOutput "defaultApp" "defaultAppBuilder"
// mkOutput "devShell" "devShellBuilder"
// mkOutput "checks" "checksBuilder"
// mkOutput "packages"
// mkOutput "defaultPackage"
// mkOutput "apps"
// mkOutput "defaultApp"
// mkOutput "devShell"
// mkOutput "checks"
)
# produces attrset in the shape of
# { nixosConfigurations = {}; darwinConfigurations = {}; ... }
# according to profile.output or the default `nixosConfigurations`
// foldHosts (attrValues (mapAttrs configurationBuilder hosts))
)

48 changes: 25 additions & 23 deletions tests/channel-patching/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,43 @@
})
];

# Using patched channel in builder
packagesBuilder = channels: {
inherit (channels.nixpkgs) flake-utils-plus-test;
};

outputsBuilder = channels: {

packages = {
# Using patched channel
inherit (channels.nixpkgs) flake-utils-plus-test;
};



######################
### Test execution ###
######################
######################
### Test execution ###
######################

checksBuilder = channels:
let
hostConfig = self.nixosConfigurations.PatchedHost.config;
in
{
checks =
let
hostConfig = self.nixosConfigurations.PatchedHost.config;
in
{

# Patched package gets passed to `packageBuilder`
patchedPackageGetsPassedToBuilders = isEqual self.packages.x86_64-linux.flake-utils-plus-test.pname "hello";
# Patched package gets passed to `packageBuilder`
patchedPackageGetsPassedToBuilders = isEqual self.packages.x86_64-linux.flake-utils-plus-test.pname "hello";

# Modules (and lib) from patched nixpkgs are used
patchedModuleAndFunctionWorks = isEqual hostConfig.patchedModule.test "using patched module via patched function";
# Modules (and lib) from patched nixpkgs are used
patchedModuleAndFunctionWorks = isEqual hostConfig.patchedModule.test "using patched module via patched function";

# `channelsConfig.*` is used
globalChannelConfigWorks = hasKey hostConfig.nixpkgs.pkgs.config "allowBroken";
# `channelsConfig.*` is used
globalChannelConfigWorks = hasKey hostConfig.nixpkgs.pkgs.config "allowBroken";

# `channels.nixpkgs.config.*` is also used
channelSpecificConfigWorks = hasKey hostConfig.nixpkgs.pkgs.config "allowUnfree";
# `channels.nixpkgs.config.*` is also used
channelSpecificConfigWorks = hasKey hostConfig.nixpkgs.pkgs.config "allowUnfree";

# `options.nixpkgs.config.*` is also used
modulesNixpkgsConfigWorks = hasKey hostConfig.nixpkgs.pkgs.config "packageOverrides";
# `options.nixpkgs.config.*` is also used
modulesNixpkgsConfigWorks = hasKey hostConfig.nixpkgs.pkgs.config "packageOverrides";

};
};
};


};
Expand Down
91 changes: 91 additions & 0 deletions tests/derivation-outputs-old/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
inputs.utils.url = path:../../;

outputs = inputs@{ self, nixpkgs, utils }:
let
mkApp = utils.lib.mkApp;

packagePname = drvKey: self.packages.x86_64-linux.${drvKey}.pname;

appHasSuffix = drvKey: suffix: nixpkgs.lib.hasSuffix suffix self.apps.x86_64-linux.${drvKey}.program;

defaultAppHasSuffix = suffix: nixpkgs.lib.hasSuffix suffix self.defaultApp.x86_64-linux.program;

pnameFromOutput = output: self.${output}.x86_64-linux.pname;
in
utils.lib.systemFlake {
inherit self inputs;
supportedSystems = [ "x86_64-linux" ];
channels.nixpkgs.input = nixpkgs;



#################
### Test Data ###
#################

defaultPackageBuilder = channels: channels.nixpkgs.coreutils;

packagesBuilder = channels: {
inherit (channels.nixpkgs) coreutils;
};

# Should Get merged with `packagesBuilder`
packages.x86_64-linux.coreutils2 = self.pkgs.x86_64-linux.nixpkgs.coreutils;



appsBuilder = channels: {
coreutils = mkApp {
drv = channels.nixpkgs.coreutils;
exePath = "/bin/nice";
};
};


defaultAppBuilder = channels: mkApp {
drv = channels.nixpkgs.coreutils;
exePath = "/bin/nice";
};

devShellBuilder = channels: channels.nixpkgs.mkShell {
pname = "super-shell";
};




######################
### Test execution ###
######################

checksBuilder = channels:
let
isTrue = cond:
if cond
then channels.nixpkgs.runCommandNoCC "success" { } "echo success > $out"
else channels.nixpkgs.runCommandNoCC "failure" { } "exit 1";
in
{

# Packages
defaultPackageValid = isTrue (pnameFromOutput "defaultPackage" == "coreutils");

packagesValid = isTrue (packagePname "coreutils" == "coreutils");

packagesMerged = isTrue (packagePname "coreutils2" == "coreutils");


# Apps
appsValid = isTrue (appHasSuffix "coreutils" "nice");

defaultAppValid = isTrue (defaultAppHasSuffix "nice");

# Devshell
devshellValid = isTrue (pnameFromOutput "devShell" == "super-shell");

};

};
}

Loading