Is there some envvar or similar that I can use to configure the location of ~/.config/nixpkgs/config.nix ?
Yes, the default Nixpkgs config file location can be set with NIXPKGS_CONFIG.
This will not affect invocations of Nixpkgs where the config parameter is set, such as
import <nixpkgs> { config = ...; }
or invocations of Nixpkgs by NixOS.
Related
I have a variable that I need to add as Jenkins Environment Variable so that the $jenkins_home/init.d groovy scripts can use them. I have this variable set in the host machine(where iam installing jenkins) - Amazon linux EC2 via a .sh file in /etc/profile.d and sourceing the /etc/profile . This did not help as I am not able to access the variable from jenkins. Can anybody please help on how I can achieve this.
In Jenkins, you can setup the global variables using the below given groovy script. You will have to use this in a shell script that you use to provision a jenkins server. This should be executed right after install so that, jenkins can use then while running jobs.
The below given groovy script creates the environment variables one by one,
createGlobalEnvironmentVariables('Var1','DummyValue')
This adds a global variable with name as Var1 and value as DummyValue
If you have multiple values, you can use the ones given below.
envVars.put("Key1", "Value1)
envVars.put("Key2", "Value2)
before calling instance.save(). We have a shell script file that has this and many other functions to install and configure jenkins in a single shot.
import hudson.EnvVars;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
import hudson.util.DescribableList;
import jenkins.model.Jenkins;
public createGlobalEnvironmentVariables(String key, String value){
Jenkins instance = Jenkins.getInstance();
DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = instance.getGlobalNodeProperties();
List<EnvironmentVariablesNodeProperty> envVarsNodePropertyList = globalNodeProperties.getAll(EnvironmentVariablesNodeProperty.class);
EnvironmentVariablesNodeProperty newEnvVarsNodeProperty = null;
EnvVars envVars = null;
if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
globalNodeProperties.add(newEnvVarsNodeProperty);
envVars = newEnvVarsNodeProperty.getEnvVars();
} else {
envVars = envVarsNodePropertyList.get(0).getEnvVars();
}
envVars.put(key, value)
instance.save()
}
createGlobalEnvironmentVariables('Var1','DummyValue')
How can we obtain upstream environment variables in jenkins scripted pipeline?
While going through documentation and came across getBuildCauses and upstreamBuilds. And bit googling came across some cases of these functions
def causes = currentBuild.getBuildCauses()
def upstream = currentBuild.rawBuild.getCause(hudson.model.Cause$UpstreamCause)
def upstream = currentBuild.upstreamBuilds
unfortunately, none of the implementation obtain environment variables from the upstream. Can some one demonstrated simple scripted pipe line example that prints upstream environment variables ? Any help is greatly appreciated.
From the Javadoc of UpstreamCause:
getUpstreamRun
#CheckForNull
public Run<?,?> getUpstreamRun()
Since: 1.505
This might return the object representing the Run that triggered your run (and so was its UpstreamCause).
From the Javadoc of Run:
EnvVars getEnvironment()
Deprecated.
as of 1.305 use getEnvironment(TaskListener)
-----
EnvVars getEnvironment(TaskListener listener)
Returns the map that contains environmental variables to be used
for launching processes for this build.
-----
Map<String,String> getEnvVars()
Deprecated.
as of 1.292 Use getEnvironment(TaskListener) instead.
Hope this might get you started.
def upStreamBuilds = currentBuild.upstreamBuilds
if (!upStreamBuilds.isEmpty()) {
// Only immediate upstream
Run<?,?> upstream = upStreamBuilds.get(0).getRawBuild()
def upstreamEnvVars = upstream.getEnvironment(TaskListener.NULL)
}
I searched a lot for this problem but couldn't find working solution anywhere. Can anybody please help me out? I want to get already existing env vars value through jenkins script console.
You need to distinguish:
build environment variables:
def myVar = build.getBuildVariables().get('myVar')
system environment variables:
System.getenv('MY_VARIABLE')
If you see
groovy.lang.MissingPropertyException: No such property: manager for class: Script1
Check this answer, and define build first:
import hudson.model.*
def build = Thread.currentThread().executable
def buildNumber = build.number
According to this answer, in order to access env vars from Jenkins script console, do as follows :
import jenkins.model.*;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.EnvVars;
jenkins = Jenkins.instance;
EnvironmentVariablesNodeProperty prop = jenkins.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class)
EnvVars env = prop.getEnvVars()
def myVariable = env['MY_VAR']
The env vars listed in http://<JENKINS_URL>/env-vars.html are available for each build. In order to access these variables in the Jenkins script console you need to define first the build :
build = Jenkins.instance.getItemByFullName('JOB_NAME').getBuildByNumber(BUILD_NUMBER)
envvars = build.getEnvironment()
envvars.each{envvar ->
println envvar
}
After adding the unstable channel
nix-channel --add https://nixos.org/channels/nixpkgs-unstable unstable
I added an overlay under ~/.config/nixpkgs/overlays/package-upgrades/default.nix
self: super:
let
unstable = import <unstable> {};
in {
jbake = unstable.jbake;
}
This overlay is added to home.nix
nixpkgs.overlays = [ (import ./overlays/package-upgrades) ];
When I run home-manager switch there is an error
0 + john#n1 nixpkgs $ home-manager switch
Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS
The entire configuration can be found here.
How can I upgrade a single attribute from unstable using home-manager and an overlay?
This thread on nixos discourse seems relevant. It appears the overlay also gets applied when importing unstable, resulting in an infinite recursion. Try something like:
let
unstable = import <unstable> {};
in {
home.packages = with pkgs; [
...
] ++ (with unstable; [
jbake
]);
}
I need to monitor what are the changes going with a job on jenkins(update the changes to a file). Need to list the env variables of a job. JOB_NAME,BUILD_NUMBER,BUILD_STATUS,GIT_URL for that build(all the builds of a job). I didn't find out a good example with the groovy. What is the best way to fetch all the info?
build.getEnvironment(listener) should get you what you need
Depending on what you would like to achieve there are at least several approaches to retrieve and save environment variables for:
current build
all past builds
Get environments variables for current build (from slave)
Execute Groovy script
// Get current environment variables and save as
// a file in $WORKSPACE.
new File(".",'env.txt').withWriter('utf-8') { writer ->
System.getenv().each { key, value ->
writer.writeLine("${key}:${value}")
}
}
Using Groovy Plug-in.
Get environment variables for current build (from master)
Execute system Groovy script
// Get current environment variables and save as
// a file in $WORKSPACE.
import hudson.FilePath
def path = "env-sys.txt"
def file = null
if (build.workspace.isRemote()) {
file = new FilePath(build.workspace.channel, build.workspace.toString() + "/" + path)
} else {
file = new FilePath(build.workspace.toString() + "/" + path)
}
def output = ""
build.getEnvironment(listener).each { key, value ->
output += "${key}:${value}\n"
}
file.write() << output
Using Groovy Plug-in.
Environment variables returned by Groovy scripts are kept in map. If you don't need all of them, you can access individual values using standard operators/methods.
Get environment variables for all past builds (from master)
This approach expecst that you have installed EnvInject Plug-in and have access to $JENKINS_HOME folder:
$ find . ${JENKINS_HOME}/jobs/[path-to-your-job] -name injectedEnvVars.txt
...
ps. I suspect that one could analyze EnvInject Plug-in API and find a way to extract this information directly from Java/Groovy code.
Using EnvInject Plug-in.
To look for only specific variables you can utilize find, grep and xargs tools .
You can use below script to get the Environment Variables
def thread = Thread.currentThread()
def build = thread.executable
// Get build parameters
def buildVariablesMap = build.buildVariables
// Get all environment variables for the build
def buildEnvVarsMap = build.envVars
String jobName = buildEnvVarsMap?.JOB_NAME // This is for JOB Name env variable.
Hope it helps!