Skip to content

Commit

Permalink
packages: add support for package.nix (#5)
Browse files Browse the repository at this point in the history
Change the heuristic to load packages.

If no `pkgs/<name>/default.nix` exists,
  fallback on loading `callPackage pkgs/<name>/package.nix { }`

This allows copying packages from nixpkgs/pkgs/by-name easily into the
project.
  • Loading branch information
zimbatm authored May 29, 2024
1 parent a6e5cea commit 1bbff01
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
49 changes: 36 additions & 13 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ let

# Get paths to directories
onlyDirs = lib.filterAttrs (name: type: type == "directory") entries;
dirPaths = lib.mapAttrs (name: _: path + "/${name}") onlyDirs;
dirPaths = lib.mapAttrs (name: type: {
path = path + "/${name}";
inherit type;
}) onlyDirs;

# Get paths to nix files, where the name is the basename of the file without the .nix extension
nixPaths = builtins.removeAttrs (lib.mapAttrs' (
Expand All @@ -69,7 +72,10 @@ let
in
{
name = if type == "directory" || nixName == null then "__junk" else (builtins.head nixName);
value = path + "/${name}";
value = {
path = path + "/${name}";
type = type;
};
}
) entries) [ "__junk" ];

Expand All @@ -78,6 +84,8 @@ let
in
lib.optionalAttrs (builtins.pathExists path) (fn combined);

entriesPath = entries: lib.mapAttrs (name: { path, type }: path);

# Prefixes all the keys of an attrset with the given prefix
withPrefix =
prefix:
Expand Down Expand Up @@ -159,7 +167,8 @@ let
};

loadHost =
name: path:
name:
{ path, type }:
if builtins.pathExists (path + "/configuration.nix") then
loadNixOS (path + "/configuration.nix")
else if builtins.pathExists (path + "/darwin-configuration.nix") then
Expand All @@ -183,10 +192,10 @@ let
);

modules = {
common = importDir (src + "/modules/common") lib.id;
darwin = importDir (src + "/modules/darwin") lib.id;
home = importDir (src + "/modules/home") lib.id;
nixos = importDir (src + "/modules/nixos") lib.id;
common = importDir (src + "/modules/common") entriesPath;
darwin = importDir (src + "/modules/darwin") entriesPath;
home = importDir (src + "/modules/home") entriesPath;
nixos = importDir (src + "/modules/nixos") entriesPath;
};
in
# FIXME: maybe there are two layers to this. The blueprint, and then the mapping to flake outputs.
Expand Down Expand Up @@ -214,7 +223,17 @@ let

packages = importDir (src + "/pkgs") (
entries:
eachSystem (args: lib.mapAttrs (pname: path: import path (args // { inherit pname; })) entries)
eachSystem (
{ pkgs, ... }@args:
lib.mapAttrs (
pname:
{ path, type }:
if type != "directory" || !builtins.pathExists (path + "/default.nix") then
pkgs.callPackage "${toString path}/package.nix" { }
else
import path (args // { inherit pname; })
) entries
)
);

darwinConfigurations = hostsByCategory.darwinConfigurations or { };
Expand All @@ -228,11 +247,15 @@ let

templates = importDir (src + "/templates") (
entries:
lib.mapAttrs (name: path: {
path = path;
# FIXME: how can we add something more meaningful?
description = name;
}) entries
lib.mapAttrs (
name:
{ path, type }:
{
path = path;
# FIXME: how can we add something more meaningful?
description = name;
}
) entries
);

checks = eachSystem (
Expand Down
17 changes: 8 additions & 9 deletions pkgs/bp/default.nix → pkgs/bp/package.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
pname,
pkgs,
flake,
...
nixos-rebuild,
runCommand,
writeShellApplication,
}:
let
pb = pkgs.writeShellApplication {
name = pname;
pb = writeShellApplication {
name = "bp";
runtimeInputs = [

];
Expand All @@ -19,18 +18,18 @@ let
shift
# Allow running the command as a user
export SUDO_USER=1
echo ${pkgs.nixos-rebuild}/bin/nixos-rebuild --flake ${flake} "$@"
echo ${nixos-rebuild}/bin/nixos-rebuild --flake . switch "$@"
;;
*)
echo "Usage: ${pname} [switch]"
echo "Usage: bp [switch]"
;;
esac
'';

meta = {
description = "ignore me, this is not ready";

tests.does-it-run = pkgs.runCommand "${pname}-does-it-run" { } ''
tests.does-it-run = runCommand "bp-does-it-run" { } ''
${pb}/bin/bp --help > $out
'';
};
Expand Down

0 comments on commit 1bbff01

Please sign in to comment.