Skip to content

Commit

Permalink
Fixed rebase mishap
Browse files Browse the repository at this point in the history
  • Loading branch information
SpiderUnderUrBed committed Dec 7, 2024
1 parent 22122d3 commit 181cc46
Showing 1 changed file with 180 additions and 148 deletions.
328 changes: 180 additions & 148 deletions hm-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
}:
let
cfg = config.programs.nixcord;

inherit (lib)
mkEnableOption
mkOption
Expand Down Expand Up @@ -34,64 +33,117 @@ let
buildWebExtension = false;
});

applyPostPatch = pkg:
pkg.overrideAttrs (oldAttrs: {
# passthru = {
# userPlugins = userPluginsDirectory;
# };
outputs = [];
postPatch = ''
applyPostPatch = pkg: pkg.overrideAttrs (oldAttrs: {
postPatch = ''
ln -s ${userPluginsDirectory} src/userplugins
'';
});

patchedVencord = lib.traceValFn (d: d.outPath) (applyPostPatch vencordPkgs);

dop = with types; coercedTo package (a: a.outPath) pathInStore;

recursiveUpdateAttrsList =
list:
if (builtins.length list <= 1) then
(builtins.elemAt list 0)
# Define regular expressions for GitHub and Git URLs
regexGithub = "github:([[:alnum:].-]+)/([[:alnum:]/-]+)/([0-9a-f]{40})";
regexGit = "git[+]file:///([^/]+/)*([^/?]+)(\\?ref=[a-f0-9]{40})?$";

# Define coercion functions for GitHub and Git
coerceGithub = value: let
matches = builtins.match regexGithub value;
owner = builtins.elemAt matches 0;
repo = builtins.elemAt matches 1;
rev = builtins.elemAt matches 2;
in builtins.fetchGit {
url = "https://github.com/${owner}/${repo}";
inherit rev;
};

coerceGit = value: let
# Match using regex, assuming regexGit is defined and captures groups correctly
matches = builtins.match regexGit value;

# Set rev only if matches are found
rev = if matches != null then
let
rawRev = builtins.elemAt matches 2;
in
if rawRev != null && builtins.substring 0 5 rawRev == "?ref="
then builtins.substring 5 (builtins.stringLength rawRev) rawRev
else null
else null;

# Set filepath only if matches are found
filepath = if matches != null then
let
startOffset = 4; # Remove 4 characters from the beginning
endOffset = 45; # Remove 45 characters from the end
fullLength = builtins.stringLength value;
adjustedPathLength = fullLength - startOffset - endOffset;
in
builtins.substring startOffset adjustedPathLength value
else null;

in if filepath != null then
# Call fetchGit only if filepath is valid
builtins.fetchGit (
let
# Only include rev if it's non-null and non-empty
revCondition = if rev != null && rev != "" then { rev = rev; } else {};
in {
url = filepath;
ref = "main";
} // revCondition
)
else
throw "Failed to extract a valid filepath from the given value";

# Mapper function that applies coercion based on the regex match
pluginMapper = plugin:
if builtins.match regexGithub plugin != null then
coerceGithub plugin
else if builtins.match regexGit plugin != null then
coerceGit plugin
else if lib.attrsets.isDerivation plugin then
plugin
else
recursiveUpdateAttrsList (
[
(attrsets.recursiveUpdate (builtins.elemAt list 0) (builtins.elemAt list 1))
]
++ (lists.drop 2 list)
);
builtins.path {
name = "plugin";
path = builtins.toPath plugin;
};

applyPostPatch =
pkg:
pkg.overrideAttrs {
postPatch = lib.concatLines (
lib.optional (cfg.userPlugins != { }) "mkdir -p src/userplugins"
++ lib.mapAttrsToList (
name: path:
"ln -s ${lib.escapeShellArg path} src/userplugins/${lib.escapeShellArg name} && ls src/userplugins"
) cfg.userPlugins
);
};
recursiveUpdateAttrsList = list:
if (builtins.length list <= 1) then (builtins.elemAt list 0) else
recursiveUpdateAttrsList ([
(attrsets.recursiveUpdate (builtins.elemAt list 0) (builtins.elemAt list 1))
] ++ (lists.drop 2 list));

defaultVencord = applyPostPatch (
pkgs.callPackage ./vencord.nix {
inherit (pkgs)
curl
esbuild
fetchFromGitHub
git
jq
lib
nix-update
nodejs
pnpm
stdenv
writeShellScript
;
buildWebExtension = false;
}
);
in
pluginDerivations = lib.mapAttrs (_: plugin: pluginMapper plugin) cfg.userPlugins;
#apiPath = vencordPkgs.outDir;
apiPath = vencordPkgs.src;
buildDirs = pluginDerivations: lib.mapAttrsToList (name: pluginDir:
let
fullPath = "${pluginDir}";

# Check for a Nix expression and build if present
buildIfExists = if builtins.pathExists "${fullPath}/default.nix" || builtins.pathExists "${fullPath}/shell.nix" then
import fullPath {
inherit pkgs
apiPath;
}

else
pluginDir;
in
# Return an attribute set with a `name` and `path` for linkFarm
{ name = name; path = buildIfExists; }
) pluginDerivations;
# Build the user plugins directory with linkFarm
userPluginsDirectory = pkgs.linkFarm "userPlugins" (buildDirs pluginDerivations);

in
{

#inherit patchedVencord;
options.programs.nixcord = {
enable = mkEnableOption "Enables Discord with Vencord";
discord = {
Expand Down Expand Up @@ -269,30 +321,24 @@ in
for both vencord and vesktop
'';
};
userPlugins =
let
regex = "github:([[:alnum:].-]+)/([[:alnum:]/-]+)/([0-9a-f]{40})";
coerce =
value:
let
matches = builtins.match regex value;
owner = builtins.elemAt matches 0;
repo = builtins.elemAt matches 1;
rev = builtins.elemAt matches 2;
in
builtins.fetchGit {
url = "https://github.com/${owner}/${repo}";
inherit rev;
};
in
mkOption {
type = with types; attrsOf (coercedTo (strMatching regex) coerce dop);
description = "User plugin to fetch and install. Note that any json required must be enabled in extraConfig";
default = { };
example = {
someCoolPlugin = "github:someUser/someCoolPlugin/someHashHere";
};
userPlugins = lib.mkOption {
description = "User plugin to fetch and install. Note that any JSON required must be enabled in extraConfig.";

# Define the type of userPlugins with validation
type = types.attrsOf (types.oneOf [
(types.strMatching regexGithub) # GitHub URL pattern
(types.strMatching regexGit) # Git URL pattern
types.package # Nix packages
types.path # Nix paths
]);

# Example usage of the userPlugins option
example = {
someCoolPlugin = "github:someUser/someCoolPlugin/someHashHere"; # GitHub example
anotherCoolPlugin = "git+file:///path/to/repo?rev=abcdef1234567890abcdef1234567890abcdef12"; # File path example
};
};

parseRules = {
upperNames = mkOption {
type = with types; listOf str;
Expand Down Expand Up @@ -336,85 +382,71 @@ in
};
};

config =
let
parseRules = cfg.parseRules;
inherit (pkgs.callPackage ./lib.nix { inherit lib parseRules; })
mkVencordCfg
;
config = let
parseRules = cfg.parseRules;
inherit (pkgs.callPackage ./lib.nix { inherit lib parseRules; })
mkVencordCfg;
vencord = patchedVencord;

vencord = applyPostPatch cfg.discord.vencord.package;

isQuickCssUsed =
appConfig:
(cfg.config.useQuickCss || appConfig ? "useQuickCss" && appConfig.useQuickCss)
&& cfg.quickCss != "";
in
mkIf cfg.enable (mkMerge [
isQuickCssUsed = appConfig: (cfg.config.useQuickCss || appConfig ? "useQuickCss" && appConfig.useQuickCss) && cfg.quickCss != "";
in mkIf cfg.enable (mkMerge [
{
home.packages = [
(mkIf cfg.discord.enable (cfg.discord.package.override {
withVencord = cfg.discord.vencord.enable;
withOpenASAR = cfg.discord.openASAR.enable;
inherit vencord;
}))
(mkIf cfg.vesktop.enable (cfg.vesktop.package.override {
withSystemVencord = true;
inherit vencord;
}))
];
}
(mkIf cfg.discord.enable (mkMerge [
# QuickCSS
(mkIf (isQuickCssUsed cfg.vencordConfig) {
home.file."${cfg.configDir}/settings/quickCss.css".text = cfg.quickCss;
})
# Vencord Settings
{
home.packages = [
(mkIf cfg.discord.enable (
cfg.discord.package.override {
withVencord = cfg.discord.vencord.enable;
withOpenASAR = cfg.discord.openASAR.enable;
inherit vencord;
}
))
(mkIf cfg.vesktop.enable (
cfg.vesktop.package.override {
withSystemVencord = true;
inherit vencord;
}
))
];
home.file."${cfg.configDir}/settings/settings.json".text =
builtins.toJSON (mkVencordCfg (
recursiveUpdateAttrsList [ cfg.config cfg.extraConfig cfg.vencordConfig ]
));
}
(mkIf cfg.discord.enable (mkMerge [
# QuickCSS
(mkIf (isQuickCssUsed cfg.vencordConfig) {
home.file."${cfg.configDir}/settings/quickCss.css".text = cfg.quickCss;
})
# Vencord Settings
{
home.file."${cfg.configDir}/settings/settings.json".text = builtins.toJSON (
mkVencordCfg (recursiveUpdateAttrsList [
cfg.config
cfg.extraConfig
cfg.vencordConfig
])
);
}
# Client Settings
(mkIf (cfg.discord.settings != { }) {
home.file."${cfg.discord.configDir}/settings.json".text = builtins.toJSON mkVencordCfg cfg.discord.settings;
})
]))
(mkIf cfg.vesktop.enable (mkMerge [
# QuickCSS
(mkIf (isQuickCssUsed cfg.vesktopConfig) {
home.file."${cfg.vesktop.configDir}/settings/quickCss.css".text = cfg.quickCss;
})
# Vencord Settings
{
home.file."${cfg.vesktop.configDir}/settings/settings.json".text = builtins.toJSON (
mkVencordCfg (recursiveUpdateAttrsList [
cfg.config
cfg.extraConfig
cfg.vesktopConfig
])
);
}
# Vesktop Client Settings
(mkIf (cfg.vesktop.settings != { }) {
home.file."${cfg.vesktop.configDir}/settings.json".text = builtins.toJSON mkVencordCfg cfg.vesktopSettings;
})
# Vesktop Client State
(mkIf (cfg.vesktop.state != { }) {
home.file."${cfg.vesktop.configDir}/state.json".text = builtins.toJSON mkVencordCfg cfg.vesktopState;
})
]))
# Warnings
# Client Settings
(mkIf (cfg.discord.settings != {}) {
home.file."${cfg.discord.configDir}/settings.json".text =
builtins.toJSON mkVencordCfg cfg.discord.settings;
})
]))
(mkIf cfg.vesktop.enable (mkMerge [
# QuickCSS
(mkIf (isQuickCssUsed cfg.vesktopConfig) {
home.file."${cfg.vesktop.configDir}/settings/quickCss.css".text = cfg.quickCss;
})
# Vencord Settings
{
warnings = import ./warnings.nix { inherit cfg mkIf; };
home.file."${cfg.vesktop.configDir}/settings/settings.json".text =
builtins.toJSON (mkVencordCfg (
recursiveUpdateAttrsList [ cfg.config cfg.extraConfig cfg.vesktopConfig ]
));
}
]);
}
# Vesktop Client Settings
(mkIf (cfg.vesktop.settings != {}) {
home.file."${cfg.vesktop.configDir}/settings.json".text =
builtins.toJSON mkVencordCfg cfg.vesktopSettings;
})
# Vesktop Client State
(mkIf (cfg.vesktop.state != {}) {
home.file."${cfg.vesktop.configDir}/state.json".text =
builtins.toJSON mkVencordCfg cfg.vesktopState;
})
]))
# Warnings
{
warnings = import ./warnings.nix { inherit cfg mkIf; };
}
]);
}

0 comments on commit 181cc46

Please sign in to comment.